Results 1 to 3 of 3

Thread: sed non-sequential line numbers

  1. #1
    Join Date
    Dec 2006
    Beans
    775

    sed non-sequential line numbers

    I am not quite sure how to sed this:

    Substitue "A" for "B" in lines 5 and 11 and 34 and 92 and 108 within test_file.

    I am able to substitue "A" for "B" in a range of lines within test_file:
    Code:
    sed -r '5,108 s/B/A/' -i test_file
    I am also able to subsitute "A" for "B" in a single line within test_file:
    Code:
    sed -r '34 s/B/A/' -i test_file
    Thanks for any guidance!

  2. #2
    Join Date
    Jan 2008
    Beans
    4,757

    Re: sed non-sequential line numbers

    Quote Originally Posted by maclenin View Post
    I am not quite sure how to sed this:

    Substitue "A" for "B" in lines 5 and 11 and 34 and 92 and 108 within test_file.

    I am able to substitue "A" for "B" in a range of lines within test_file:
    Code:
    sed -r '5,108 s/B/A/' -i test_file
    I am also able to subsitute "A" for "B" in a single line within test_file:
    Code:
    sed -r '34 s/B/A/' -i test_file
    Thanks for any guidance!
    Don't think it can be done without using -e for each regexp.

    Code:
    -e '5 s/B/A' -e '11 s/B/A' -etc...

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: sed non-sequential line numbers

    if you want to keep it short and not duplicate the expression you could do something like this:
    Code:
    $ s='s/A/A*/'
    $ for i in {1..10}; do echo A; done | sed "5$s; 7$s; 9$s"
    A
    A
    A
    A
    A*
    A
    A*
    A
    A*
    A
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •