PDA

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



john_spiral
November 25th, 2009, 11:46 AM
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.

mo.reina
November 25th, 2009, 11:58 AM
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....


,,./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

john_spiral
November 25th, 2009, 12:01 PM
I would like to exclude those not colored, if possible.

mo.reina
November 25th, 2009, 12:07 PM
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 ,,?

john_spiral
November 25th, 2009, 12:15 PM
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.

mo.reina
November 25th, 2009, 12:49 PM
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:

,,./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.

john_spiral
November 25th, 2009, 12:52 PM
looking good I'll give it a try, thank you so much for your assistance!

mo.reina
November 25th, 2009, 12:54 PM
remember that sed doesn't write the changes to the file... you'll have to use the w option or redirect output with >

Arndt
November 25th, 2009, 01:28 PM
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.

ghostdog74
November 25th, 2009, 01:31 PM
use sed for simple substitution. anything beyond, sed regex becomes ugly and hard to read.

here's gawk


awk '/^\.\//{
c=3;
print ",,"$0;next
}
c-->0 && !/^\.\// {
print",,"$0; next
}{print}' file

output


$ ./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