Search string and return only the 5th line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search string and return only the 5th line
# 1  
Old 02-01-2009
Search string and return only the 5th line

I have looked at almost every posting on here regarding, grep, sed, awk, nawk, perl methods to perform this function, but they are just not returning the results I hope to see.

Example File:

abcdef
123456
ghijkl
7890123
7890123
7890123
7890123
mnopqrs
456789

This is the most effective one line command that I have found thus far:

awk '{a[NR]=$0}$0~/7890123/{printf("%s\n%s\n%s \n",a[NR-4],a[NR-2],a[NR-1])}' <filename>

RESULTS:

123456
ghijkl

ghijkl
7890123
123456
7890123
7890123
ghijkl
7890123
7890123

As you can see, it returns the string I want '123456', but the downfall is it returns the value multiple times and returns a lot of uneeded lines. I would just like to return one instance of '123456'.

Any help would be appreciated?

# 2  
Old 02-01-2009
Quote:
Originally Posted by doza22
I have looked at almost every posting on here regarding, grep, sed, awk, nawk, perl methods to perform this function, but they are just not returning the results I hope to see.

What, exactly, is that function?
Quote:

Example File:

abcdef
123456
ghijkl
7890123
7890123
7890123
7890123
mnopqrs
456789

This is the most effective one line command that I have found thus far:

awk '{a[NR]=$0}$0~/7890123/{printf("%s\n%s\n%s \n",a[NR-4],a[NR-2],a[NR-1])}' <filename>

You'll get more help if you post legible code; that means using [code] tags and formatting the code legibly.

Is this what you want?

Code:
awk '{ a[NR] = $0 }
 $0 ~ /7890123/ {
     printf "%s\n%s\n%s \n", a[NR-4], a[NR-2], a[NR-1]
     exit
 }
' <filename>

# 3  
Old 02-01-2009
Thanks for the reply. Sorry, I just copy and pasted just as I pulled it off another site.

Your recommendation returned:

123456
ghijkl

Is there any way to get rid of the 'ghijkl' line?

Thanks again!
# 4  
Old 02-01-2009
Quote:
Originally Posted by doza22
Thanks for the reply. Sorry, I just copy and pasted just as I pulled it off another site.

You can learn a lot about code by simply reformatting it.
Quote:

Your recommendation returned:

123456
ghijkl

Is there any way to get rid of the 'ghijkl' line?
Code:
awk '{ a[NR] = $0 }
 $0 ~ /7890123/ {
     printf "%s\n", a[NR-2]
     exit
 }
' FILENAME

# 5  
Old 02-01-2009
Thank you very much! It works perfectly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

2. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

3. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

4. Shell Programming and Scripting

Need help in search for word and return line

Hi, I have this file format and can't seem to think of a solution. I need your help. I want to return lines which says "Record" if it's ID > 0 file.txt Record 1: :::::::::::::::::::::: :::::::::::::::::::::: ID "000001" :::::::::::::::::::::: :::::::::::::::::::::: ... (6 Replies)
Discussion started by: jakSun8
6 Replies

5. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

6. Shell Programming and Scripting

How to search for string and return binary result?

Hi, I have a problem that I am sure someone will know the answer to. Currently I have a script which returns a binary output if it finds a certain search string (in this case relating to a DRBD cluster) as follows: searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate" && echo... (3 Replies)
Discussion started by: almightybunghol
3 Replies

7. UNIX for Dummies Questions & Answers

search for string and return substring

Hi, I have a file with the following contents: I need to create a script or search command that will search for this string 'ENDC' in the file. This string is unique and only occurs in one record. Once it finds the string, I would like it to return positions 101-109 ( this is the date of... (0 Replies)
Discussion started by: Lenora2009
0 Replies

8. UNIX for Dummies Questions & Answers

Search for string and return lines above and below

Hi, I have a file with the following contents: COMPANY ABCD TRUCKER JOE SMITH TRUCK 456 PLATE A12345678 TRUCKER BILL JONES TRUCK 789 PLATE B7894561 I need to create a script or search command that will search for this string '789' in the file. This string is unique and only... (7 Replies)
Discussion started by: doza22
7 Replies

9. Shell Programming and Scripting

String search and return value from column

Dear All I had below mention file as my input file. 87980457 Jan 12 2008 2:00AM 1 60 BSC1 81164713 Jan 12 2008 3:00AM 1 60 BSC2 78084521 Jan 12 2008 4:00AM 1 60 BSC3 68385193... (3 Replies)
Discussion started by: jaydeep_sadaria
3 Replies

10. Shell Programming and Scripting

return line below string grepp'ed for

Hi there Does anybody know how to get grep to return the line underneath the string i am grepping for ie, if i have this file hello everybody how are you and i issue #cat file | grep how I would like it to return the line below (in this case the string "are") Is this... (2 Replies)
Discussion started by: hcclnoodles
2 Replies
Login or Register to Ask a Question