Computing.Net > Forums > Unix > awk script to extract text

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

awk script to extract text

Reply to Message Icon

Name: jka
Date: January 8, 2004 at 13:41:16 Pacific
OS: SunOS
CPU/Ram: RISC
Comment:

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
END

AAA222
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
END

I 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
END

Let us say, there are 10 blocks with value2.
These 10 blocks are to be moved to a different file from the original file.




Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: January 8, 2004 at 15:13:33 Pacific
Reply:

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


0

Response Number 2
Name: jka
Date: January 11, 2004 at 11:04:30 Pacific
Reply:

Hi James,

The script does exactly what I was looking for.
Your help is very much appreciated.

Joseph


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Core files too small Problem with rmdir comman...



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: awk script to extract text

Shell script to extract text www.computing.net/answers/unix/shell-script-to-extract-text/3999.html

shell script to extract information www.computing.net/answers/unix/shell-script-to-extract-information/6276.html

How to call other shell/awk script www.computing.net/answers/unix/how-to-call-other-shellawk-script-/5571.html