-4

I have short text file, where i should make output of data using special pattern. My file:

99 test1
88 test2
10 test3
11 test1
12 test1
13 test2
14 test3
17 test1
18 test4

One by one, from test1 to test2 and to test3. So... I have written the comand:

sed '/test1.*\|test2.*\|test3/!d' filename

And in output i have result:

99 test1
88 test2
10 test3
11 test1
12 test1
13 test2
14 test3
17 test1

In this case, i have lines, that i not need:

11 test1

17 test1

This lines don't go one after one. How i can do result, that i need ? Please help me. Thanks for your attention. i Should get this result:

99 test1
88 test2
10 test3
12 test1
13 test2
14 test3
Valeriu
  • 1
  • 1

1 Answers1

0

A simple quick way, although not very elegant, is to put it in one line, break it in the desired pattern, and then filter it again with the pattern:

sed ':a;N;$!ba;s/\n/\\n/g' < filename | \
sed 's/\([0-9][0-9] test1\\n[0-9][0-9] test2\\n[0-9][0-9] test3\\n\)/\n\1\n/g' | \
egrep "[0-9][0-9] test1\\\n[0-9][0-9] test2\\\n[0-9][0-9] test3\\\n" | \
sed 's/\\n/\n/g' | grep .
  • not bad. but in output, i have: 99 test1 88 test2 10 test3. but i should have: 99 test1 88 test2 10 test3 12 test1 13 test2 14 test3 – Valeriu Feb 09 '17 at 13:35
  • Works as expected for me, maybe you have a different flavour of grep and sed. You should check the output without the last two commands (grep and sed) to see what you get and then work from there. – João Miranda Feb 09 '17 at 13:42
  • in your case, i have output first three lines, but i should get all lines in pattern (test1,test2,test3) – Valeriu Feb 09 '17 at 13:45