Find a string using grep & print the line above or below that.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a string using grep & print the line above or below that.
# 1  
Old 11-04-2010
Find a string using grep & print the line above or below that.

Hi All,

Please tell me how can I Find a string using grep & print the line above or below that in solaris?

Please share as I am unable to use grep -A or grep -B as it is not working on Solaris.

Last edited by Don Cragun; 09-20-2015 at 06:25 PM.. Reason: Add ICODE tags.
# 2  
Old 11-04-2010
in solaris you have to use nawk. please state your input and desired output.
# 3  
Old 11-04-2010
You could read the lines into an array:

Code:
$> cat infile
eins
zwei
drei
vier
fuenf
sechs
sieben
$> awk '{arr[NR]=$0; if($0 ~ /fuenf/)s=NR} END{print arr[s-2]}' infile
drei

In the above example the pattern to start at is fuenf and the offset is in -2.

Remind gc_sw note to use nawk (maybe).
# 4  
Old 11-04-2010
Code:
nawk '{if (/pattern/) {print a; getline ;print } else {a=$0}}' inputfile

# 5  
Old 11-04-2010
See https://www.unix.com/shell-programmin...p-pattern.html, use nawk of /usr/xpg4/bin/awk if on Solaris.
# 6  
Old 11-05-2010
I am on Solaris.
& I want this:
Files contains:
Code:
abc
123
def
456
def

Now, I grep def & I want all lines which are immediate above def.
Similar is that with immediate below. I have done this once with just one command but unfortunately I have forgotten.
Please check & share any command.

Last edited by Don Cragun; 09-20-2015 at 06:26 PM.. Reason: Add CODE and ICODE tags.
# 7  
Old 11-05-2010
Code:
awk '{A[NR]=$0}/def/{print A[NR-1]}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

2. SCO

Grep to ignore suffix & find end of line

In COBOL, a hyphen can be used in a field name and in a specific program some field names would be identical to others except a suffix was added--sometimes a suffix to a suffix was used. For example, assume I am looking for AAA, AAA-BBB, and AAA-BBB-CCC and don't want to look at AAA-BBB-CCC... (7 Replies)
Discussion started by: wbport
7 Replies

3. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

4. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

5. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

6. Shell Programming and Scripting

grep a string and print strings on that line

i have a file like below ABS 12234 A C 12G CFY 23865 A C 34D i want to grep "A C" then print ABS 12G 12234 CFY 34D 23865 Thanks! (4 Replies)
Discussion started by: kingpeejay
4 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

Perl : Find a string and Print full line

Hi Need a perl script to read lines in a file, scan for a string named "APPLE" and write to different file the only lines containing the matched string. (5 Replies)
Discussion started by: PrasannaKS
5 Replies

9. Shell Programming and Scripting

Grep a string and print a string from the line below it

I know how to grep, copy and paste a string from a line. Now, what i want to do is to find a string and print a string from the line below it. To demonstrate: Name 1: ABC Age: 3 Sex: Male Name 2: DEF Age: 4 Sex: Male Output: 3 Male I know how to get "3". My biggest problem is to... (4 Replies)
Discussion started by: kingpeejay
4 Replies

10. Shell Programming and Scripting

grep string and find line before

hi, i have to grep for string in file but i want to find the group of this line so i must get lines before and select the group. the file look like : ####name_groupe1 alphanumeric line alphanumeric line .. ####name_groupe2 alphanumeric line alphanumeric line .. ####name_groupe3... (4 Replies)
Discussion started by: kamel.seg
4 Replies
Login or Register to Ask a Question