Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need help with a script that does the following operation.
A text file contains the following information.
AAA111
a line of some text
another line of some text
key: value1
ENDAAA222
a line of some text
another line of some text
key: value2
END
AAA333
a line of some text
another line of some text
key: value3
ENDI want to extract multiple instances of a block of text (shown below) based on a particular key.
AAA222
a line of some text
another line of some text
key: value2
ENDLet us say, there are 10 blocks with value2.
These 10 blocks are to be moved to a different file from the original file.

If your blocks of texts were separated by blank (null) lines, then the following would work:
sed -e '/./{H;$!d;}' -e 'x;/value2/!d;' myfile > outfile
But in your example, I see that the second and third groups do not have a blank line between them. Soooooo ......
On the assumption that you are not interested in blank lines except to delimit your groups, I would first pass the file through sed to ensure that the END line is followed by a blank line, and to clean up any extraneous blank lines. Then the second sed can pick out the groups you want:
sed -e /^$/d -e /^END$/G myfile | sed -e '/./{H;$!d;}' -e 'x;/value2/!d;' > outfile

![]() |
Core files too small
|
Problem with rmdir comman...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |