grep for a line then return lines above


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep for a line then return lines above
# 1  
Old 03-05-2007
grep for a line then return lines above

Hey guys,

I just want to grep for a line then return a few lines above it I can't seem to find what im looking for on google can someone help me out?

This is on Solaris 9.....

I don't have GNU grep so -B and -A commands will not work

Last edited by kingdbag; 03-05-2007 at 06:01 PM..
# 2  
Old 03-05-2007
Grab GNU grep from sunfreeware.com

http://www.sunfreeware.com/programlistsparc9.html#grep

You can try something like this although it's convoluted.

Cheers
ZB
# 3  
Old 03-05-2007
Wow, do i really need to script something for grep to do this?

I found this and its supposed to return the above 3 lines when it finds FORWARD in the text file and it is blank...

sed -n '/FORWARD/{g;3!p;};h' crap.txt

Last edited by kingdbag; 03-05-2007 at 06:42 PM..
# 4  
Old 03-05-2007
Quote:
Originally Posted by kingdbag
Hey guys,

I just want to grep for a line then return a few lines above it I can't seem to find what im looking for on google can someone help me out?

This is on Solaris 9.....

I don't have GNU grep so -B and -A commands will not work
Code:
pattern=$1
num=$2
shift
nawk -v pattern="$pattern" -v num="$num" '
                       { x[NR % num] = $0 }
  index( $0, pattern ) {
                 n = NR % num
                 top = n + num
                 while ( n++ < top ) print x[n % num]
               }'

I have used nawk, since the default Solaris awk is a version which has been obsolete for almost 20 years.
More recent versions on Solaris, include nawk and /usr/xpg4/bin/awk.

The above version only handles strings, not regular expressions. The following one deals with regexps, but will duplicate lines if there is an overlap. (I may fix that if I have a moment or to, or perhaps someone else can do it.)

Code:
pattern=$1
num=$2
shift
nawk -v num=$num '
                       { x[NR % num] = $0 }
  /'"$pattern"'/ {
                 n = NR % num
                 top = n + num
                 while ( n++ < top ) print x[n % num]
               }'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep line with all string in the lines and not space.

I want to write the syntax so does not count line with no space. So currerntly it is showing lines as 5, but i want to show 4. # cat /tmp/mediacheck | sort -u | grep -vi " " | awk '{print $1}' | wc -l BA7552 BAA002 BAA003 BAA004 (6 Replies)
Discussion started by: Junes
6 Replies

2. Shell Programming and Scripting

Removing carriage return/line feeds on multiple lines

I would like to remove carriage returns/line feeds in a text file, but in a specific cadence: Read first line (Header Line 1), remove cr/lf at the end (replace it with a space ideally); Read the next line (Line of Text 2), leave the cr/lf intact; Read the next line, remove the cr/lf; Read... (14 Replies)
Discussion started by: tomr2012
14 Replies

3. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

4. Shell Programming and Scripting

Grep command to return all the lines between one matched pattern to another.

14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. 14:50:00- abcdefghijkl. 123456789. 15:30:00-abcdefghijkl. (3 Replies)
Discussion started by: dev_shivv
3 Replies

5. Solaris

Grep command to return all the lines from one matched pattern to another.

For example a log file looks like below- 13:30:00- abcdefghijklhjghjghjhskj. abcdefghijkl. 14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. (0 Replies)
Discussion started by: dev_shivv
0 Replies

6. UNIX for Dummies Questions & Answers

Grep to return lines not containing a character

Hello , this is my first topic cause I need your little help:( I got .txt file, and I want to find lines without letter 'a', so im writing: grep "" list.txt (list.txt is the file of course) and i have no idea why it's not working because it shows lines with a. (1 Reply)
Discussion started by: bbqtoss
1 Replies

7. UNIX for Dummies Questions & Answers

Can grep command return word instead of complete line

Hi Is there any way GREP command can return word and not complete line. My file has following data: Hello Everyone I am NitinrajSrivastava Hi Friends Welcome VrajSrivastava I am using grep 'raj' which is returning me complete line.However I want only the word having keyword 'raj'. Required... (11 Replies)
Discussion started by: dashing201
11 Replies

8. Shell Programming and Scripting

grep return more than one line

Hi, I have a lot of log files which contain lines in the format of (date info) or (info), and when I use grep to search for "date" I was able to get the (date info) line, but some info lines are a separate line after the (date info) line... for example like: (date info info info) (date... (8 Replies)
Discussion started by: a27wang
8 Replies

9. Shell Programming and Scripting

grep N lines after match and then print them on 1 line each

Hello I need some help with this job. file.txt ----- cut ---- TARGET 13/11/08 20:43:21 POINT 1 MOVE 8 772102y64312417771 TARGET 13/11/08 21:10:01 POINT 2 MOVE 5 731623jjd12njhd ----- cut ---- this is the example. i need to grep for the word TARGET and print next 4 lines like... (1 Reply)
Discussion started by: alekkz
1 Replies

10. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies
Login or Register to Ask a Question