Visit Our UNIX and Linux User Community


find string, then get the next 3 lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find string, then get the next 3 lines in a file
# 1  
Old 11-05-2007
find string, then get the next 3 lines in a file

Hi all. HPUX - /bin/sh (posix)

I am parsing a 3 field flat file, space deliminted

example data.file

acct dining mem
open 0 50
dep 50 0
close 255 0
acct plus mem
open 100 100
dep 50 0
close 323 0

I would like to find strings, then write the next 3 lines out to another file

exec < data.file
while read NAME NAME2 NAME3
do
if [ "$NAME2" = "dining" ]
then (how to I get the very next 3 lines?)
# 2  
Old 11-05-2007
Code:
#!/bin/sh

while read NAME1 NAME2 NAME3
do
        case "$NAME2" in
        dining )
                read A
                read B
                read C
                echo $A $B $C
                ;;
        plus )
                read A
                read B
                read C
                echo $A $B $C
                ;;
        * )
                exit 1
                ;;
        esac
done <data.file

# 3  
Old 11-06-2007
Hi.

The GNU grep can easily do this. However, AT&T offers cgrep which also can do "context" printing. One might want to use this approach for files that are large enough so that shell scripts might be too slow, or where you need additional features:
Code:
#!/bin/bash --posix

# @(#) s1       Demonstrate AT&T free cgrep for grep with context.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash cgrep
set -o posix
set -o | grep -i posix

echo

FILE=${1-data1}

cgrep +3 "dining" $FILE

exit 0

Producing on your data in file "data1":
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
cgrep (local) - no version provided.
posix           on

acct dining mem
open 0 50
dep 50 0
close 255 0

The AT&T source for cgrep is available at: cgrep home page -- I had no trouble downloading and compiling it on GNU/Linux, but I cannot vouch for HPUX.

It is a very useful command that can take on jobs that GNU grep normally does, plus a lot more. Best wishes ... cheers, drl
# 4  
Old 11-06-2007
How about a one-line solution with sed?

Code:
sed -ne "/ dining /{n;p;n;p;n;p;}" data.file > output.file

All you need to do is make sure that the regex "/ dining /" is sufficently strong to avoid false matches.
# 5  
Old 11-06-2007
Bug find string, then get the next 3 lines in a file

Please Try following

#!/bin/sh

val=`grep -n "string" filename |awk -F":" '{print $1}'
tail +`expr $val + 1` filename |head -3


Thanks
Sridhar SSmilie
# 6  
Old 11-06-2007
Yuk! That won't work at all. It assumes that the search term "string" is found only once in the datafile. If it is found more than once, then $val will have a multi-line and cause any 'expr $val' to fail with a syntax error.
# 7  
Old 11-07-2007
awk!!!

Hi,

This one should be ok for you.

input:
Code:
acct dining mem
open 0 50
dep 50 0
close 255 0
acct plus mem
open 100 100
dep 50 0
close 323 0
acct dining mem
open 0 50
dep 50 0
close 255 0
acct plus mem
open 100 100
dep 50 0
close 323 0

output:
Code:
open 0 50
dep 50 0
close 255 0
open 0 50
dep 50 0
close 255 0

code:
Code:
awk '
{
if ($2=="dining")
n=NR
if (NR>=n+1 && NR<=n+3)
print
}' a > c
cat c


Previous Thread | Next Thread
Test Your Knowledge in Computers #783
Difficulty: Medium
Microsoft launched its first retail version of Microsoft Windows in 1983.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

2. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

5. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

6. Shell Programming and Scripting

Find the position of lines matching string

I have a file with the below format, GS*8***** ST*1******** A* B* E* RMR*123455(This is the unique number to locate this row) F* SE*1*** GE** GS*9***** ST*2 H* J* RMR*567889(This is the unique number to locate this row) L* SE* GE***** (16 Replies)
Discussion started by: Muthuraj K
16 Replies

7. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

8. Shell Programming and Scripting

Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern: STRING1 UNIQUE_STRING1 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING2 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING3 STRING2 STRING3 (6 Replies)
Discussion started by: ilcsfe
6 Replies

9. Shell Programming and Scripting

Find a string and place two blank lines

Hi friends, I am looking for a line to find a particular string in my file and once found then replace with 2-3 blank lines before the string Example: aaa 11 bbb 1 2 3 aaa 22 bbb 4 5 6 Output (4 Replies)
Discussion started by: shaliniyadav
4 Replies

10. Shell Programming and Scripting

Script to find string and return next 2 lines

I am trying to create a script to search for a string within a file, and if found, return the next two lines. Example file:- msj mh query return this 1 return this 2 mjk mhj query return this 3 return this 4 So the script would identify the string "query" and then return the lines... (10 Replies)
Discussion started by: daveaasmith
10 Replies

Featured Tech Videos