Computing.Net > Forums > Unix > Extracting multipe lines from file

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.

Extracting multipe lines from file

Reply to Message Icon

Name: in_a_pickle
Date: October 6, 2008 at 08:21:43 Pacific
OS: AIX
CPU/Ram: NA
Product: 5.2
Comment:

I want to be able to search for a string in a text file and pull out the line that matches my string and also a number of lines immediately before the string line.
For example, if I have the file test.log whose content are:

13: ]<1111111>
42: ]<1111111>
43: ]<1111111>
52: ]<1111111>

13: ]<2222222>
42: ]<2222222>
43: ]<2222222>

13: ]<3333333>
42: ]<3333333>
43: ]<3333333>
52: ]<3333333>

I want my output to only contain the blocks that have a '52:' entry present, so my output should look like this:
13: ]<1111111>
42: ]<1111111>
43: ]<1111111>
52: ]<1111111>
13: ]<3333333>
42: ]<3333333>
43: ]<3333333>
52: ]<3333333>

I have used:
ex -R filename <<!
g/52: ]/.-4,.+3p
q
!

which will give me the previous 4 lines from where it finds '52: ]'. But 'ex' will only work on files up to a max size of approx 5505 KB.
The log file Im working on can reach sizes of 507,975 KB! I have tried to find a solution using 'AWK' but this hasnt worked out. 'grep' is not an option as it takes way too long and it will just give me the blocks that dont include '52:'.
Thanks in advance!



Sponsored Link
Ads by Google

Response Number 1
Name: in_a_pickle
Date: October 6, 2008 at 09:02:55 Pacific
Reply:

I should also add that I cannot use the grep '-A' or '-B' flags with my OS as they are not recognized on my version!


0

Response Number 2
Name: nails
Date: October 6, 2008 at 13:06:50 Pacific
Reply:

Maybe you can use Steven's "Context" script located here:

http://www.unixreview.com/documents...

It was developed for those not using the GNU grep command.


0

Response Number 3
Name: FishMonger
Date: October 6, 2008 at 18:02:58 Pacific
Reply:

perl -00 -ne 'print if /52: \]/' test.log

or

perl -00 -ne "if(/52: \]/){s/\n$//; print}" test.log
=========================================================


0

Response Number 4
Name: in_a_pickle
Date: October 7, 2008 at 02:01:33 Pacific
Reply:

Thanks for the replies. Im getting a 'Servlet Exception' error from the link http://www.unixreview.com/documents...

I think I have found a solution using AWK:
awk "\$0 ~ /52: ]/ {
cmd=\"awk 'NR>=\" NR-4 \" && NR<=\" NR+2 \"' test.log\"
system(cmd)
}" test.log

The following 'sed' also works but I cannot get my head around how or why it works! Its not customizable enough for me! sed is very tricky...
sed -e '
1{$!N;$d;}
$!N;/52: ]/!D
$!N;$d;N;p
g;$!N;$d;N;D
' test.log

Thanks again.


0

Response Number 5
Name: nails
Date: October 7, 2008 at 20:52:02 Pacific
Reply:

>>Thanks for the replies. Im getting a 'Servlet Exception' error from the link

http://www.unixreview.com/documents...

Seems to be working now. Hey, it's the internet :)


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






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: Extracting multipe lines from file

Extract multiple lines from file www.computing.net/answers/unix/extract-multiple-lines-from-file/8464.html

Process line from file -Cshell scr www.computing.net/answers/unix/process-line-from-file-cshell-scr/5072.html

extract a specific line from file www.computing.net/answers/unix/extract-a-specific-line-from-file/5381.html