Results 1 to 10 of 10

Thread: Help sed Multi-Line, searching for sequence of lines starting with specific character

  1. #1
    Join Date
    Feb 2005
    Beans
    509
    Distro
    Lubuntu 11.10 Oneiric Ocelot

    Help sed Multi-Line, searching for sequence of lines starting with specific character

    Hi,

    I've got my head round the basics of sed but I'm stumped for a solution on the below problem.

    I would like sed to replace the beginning of the colored lines with two commas. In effect sed will be searching for a sequence of lines starting with specific characters.

    A picture is worth a thousand words...

    Before

    ./path/to/file/xyz
    File is on a network share.
    Network Share name = \\server\path\
    Path = unique_value
    ./path/to/file/abc
    File is on a network share.
    Network Share name = \\server\path\
    Path = more path info
    Path = more path info
    Path = more path info
    ./path/to/file/efg
    File is on a network share.
    Network Share name = \\server\path\
    Path = more path info
    Path = more path info
    ./path/to/file/lmn
    File is on a network share.
    Network Share name = \\server\path\
    Path = more path info

    After

    , ,./path/to/file/xyz
    , ,File is on a network share.
    , ,Network Share name = \\server\path\
    , ,Path = unique_value
    , ,./path/to/file/abc
    , ,File is on a network share.
    , ,Network Share name = \\server\path\
    , ,Path = more path info
    Path = more path info
    Path = more path info
    , ,./path/to/file/efg
    , ,File is on a network share.
    , ,Network Share name = \\server\path\
    , ,Path = more path info
    Path = more path info
    , ,./path/to/file/lmn
    , ,File is on a network share.
    , ,Network Share name = \\server\path\
    , ,Path = more path info

    any help will be greatly appreciated.

  2. #2

    Re: Help sed Multi-Line, searching for sequence of lines starting with specific chara

    Code:
    sed -e 's/^\(F\)/,,\1/g' -e 's/^\(N\)/,,\1/g' -e 's/^\(P\)/,,\1/g' -e 's/^\(\.\/\)/,,\1/g'
    however this will include all lines starting with P. in your example there are some lines with P that are not colored....

    Code:
    ,,./path/to/file/xyz
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = unique_value
    ,,./path/to/file/abc
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = more path info
    ,,Path = more path info
    ,,Path = more path info
    ,,./path/to/file/efg
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = more path info
    ,,Path = more path info
    ,,./path/to/file/lmn
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = more path info

  3. #3
    Join Date
    Feb 2005
    Beans
    509
    Distro
    Lubuntu 11.10 Oneiric Ocelot

    Re: Help sed Multi-Line, searching for sequence of lines starting with specific chara

    I would like to exclude those not colored, if possible.

  4. #4

    Re: Help sed Multi-Line, searching for sequence of lines starting with specific chara

    need more information... are there always 4 lines one after the other that should have ,,? so like, first line starts with ./, and the next 3 lines have ,,?

  5. #5
    Join Date
    Feb 2005
    Beans
    509
    Distro
    Lubuntu 11.10 Oneiric Ocelot

    Re: Help sed Multi-Line, searching for sequence of lines starting with specific chara

    are there always 4 lines one after the other that should have ,,

    correct

    The sequence is:

    ./$$$$
    File is on a network share.
    Network Share name = $$$
    Path = $$$

    where $$$ changes.

    I would like to omit additional 'Path' lines with sed.

    hope this helps clarify things.

  6. #6

    Re: Help sed Multi-Line, searching for sequence of lines starting with specific chara

    Code:
     sed -e 's/^\(F\)/,,\1/g' -e 's/^\(N\)/,,\1/g' -e 's/^\(\.\/\)/,,\1/g' | sed '/,,N/{n;s/^/,,/;}'
    sed finds lines beginning with F, ./, and N and inserts ,,. then it finds the lines beginning with ,,N and inserts ,, at the beginning of the next line.

    not the most elegant solution but it's a bit late here and this was the best i could do

    output:
    Code:
    ,,./path/to/file/xyz
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = unique_value
    ,,./path/to/file/abc
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = more path info
    Path = more path info
    Path = more path info
    ,,./path/to/file/efg
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = more path info
    Path = more path info
    ,,./path/to/file/lmn
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = more path info
    shoutout to Perderabo at the unix forums, one of his posts provided that last bit of code, i just modified it to fit the scenario.

  7. #7
    Join Date
    Feb 2005
    Beans
    509
    Distro
    Lubuntu 11.10 Oneiric Ocelot

    Re: Help sed Multi-Line, searching for sequence of lines starting with specific chara

    looking good I'll give it a try, thank you so much for your assistance!

  8. #8

    Re: Help sed Multi-Line, searching for sequence of lines starting with specific chara

    remember that sed doesn't write the changes to the file... you'll have to use the w option or redirect output with >

  9. #9
    Join Date
    May 2006
    Beans
    1,787

    Re: Help sed Multi-Line, searching for sequence of lines starting with specific chara

    Quote Originally Posted by mo.reina View Post
    remember that sed doesn't write the changes to the file... you'll have to use the w option or redirect output with >
    Gnu sed does, with the option -i.

  10. #10
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Help sed Multi-Line, searching for sequence of lines starting with specific chara

    use sed for simple substitution. anything beyond, sed regex becomes ugly and hard to read.

    here's gawk
    Code:
    awk '/^\.\//{ 
        c=3;
        print ",,"$0;next
    }
    c-->0 && !/^\.\// { 
        print",,"$0; next
    }{print}' file
    output
    Code:
    $ ./shell.sh
    ,,./path/to/file/xyz
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = unique_value
    ,,./path/to/file/abc
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = more path info
    Path = more path info
    Path = more path info
    ,,./path/to/file/efg
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = more path info
    Path = more path info
    ,,./path/to/file/lmn
    ,,File is on a network share.
    ,,Network Share name = \\server\path\
    ,,Path = more path info

Tags for this Thread

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
  •