Printing 10 lines above and below the search string: help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing 10 lines above and below the search string: help needed
# 1  
Old 02-02-2010
Debian Printing 10 lines above and below the search string: help needed

Hi,

The below code will search a particular string(say false in this case) and return me 10 lines above and below the search string in a file.

"
Code:
awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print("***********************************");print;c=a;}b{r[NR%b]=$
0}' b=10 a=10 s="false"

"

Further to enhance I want to ignore if the same search string appears in those 10 lines. (Currently this is not happening. For Ex: If the search string exists in 5th line it again starts printing 10lines above and below causing some mess.)

It would be really great if someone could help me on this.

P.S: Being my first post apologies for any mistakes in my post and suggestions welcome.

Thanks,
Vimal
# 2  
Old 02-02-2010
Does your version of grep support
Code:
grep -A 10 -B 10 'mypattern'   filename

That does exaclty what you want.
# 3  
Old 02-04-2010
Hi,

Thanks a lot but I m afraid that when i execute the above command it says "-A" not a valid option.

Please suggest me a different option to try with.
# 4  
Old 02-04-2010
in solaris use below

Code:
/usr/sfw/bin/ggrep -A 10 -B 10 'mypattern'   filename

# 5  
Old 02-04-2010
Try this one:
Code:
awk '
/false/ {
  for(i=0;i<10;i++) {
    print a[(NR+i)%10]
  }
  exit
}
{a[NR%10]=$0}
' file

# 6  
Old 03-01-2010
Dear Friend,

You can use the following code also

Code:
 
  egrep -A 10 -B 10 "pattern" file

Here

-A is used to print the 10 lines after the matched patten
-B is used to print the 10 lines before the matched pattern
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 and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

2. Shell Programming and Scripting

Help in printing n number of lines if a search string matches in a file

Hi I have below script which is used to grep specific errors and if error string matches send an email alert. Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert Current code:- #!/bin/bash tail -Fn0 --retry... (2 Replies)
Discussion started by: neha0785
2 Replies

3. Shell Programming and Scripting

Printing the lines of the string with highest value

Please help . The script need to first grep for all lines with "C:" as it contains a value Here the value is 0 1,00: This , is a good script c:0 and then give output of the lines with top 3 highest value for c: 1,00: This , is a nice script c:9999 1,00: This , is a... (3 Replies)
Discussion started by: necro98
3 Replies

4. UNIX for Dummies Questions & Answers

Printing the lines using search patterns

Hi all , i need an help here.!!!! i have a file that contains /etc/passwd files from some servers. i need a script which search for presence of a user in the servers. like if i give 51144 to the script. the should be o/p Please help on this..... (4 Replies)
Discussion started by: sudharson
4 Replies

5. Shell Programming and Scripting

Printing all lines before a specific string and a custom message 2 lines after

Hello all, I need to print all the lines before a specific string and print a custom message 2 lines after that. So far I have managed to print everything up the string, inclusively, but I can't figure out how to print the 2 lines after that and the custom message. My code thus far is:... (4 Replies)
Discussion started by: SEinT
4 Replies

6. 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

7. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

8. Shell Programming and Scripting

printing lines to a file from a particular string

Hi, A very Good Evening to All, I am writing a script for my application. I have a file with 1000 lines. Among that 1000 lines i am searching for a particular string. And from that string i need to pull all the data in to a seperate file. For example the contents of my file is as below. ... (4 Replies)
Discussion started by: intiraju
4 Replies

9. Shell Programming and Scripting

Printing the lines which proceeds the particular string

Hi, We are facing some issues while finding the particular string. Our file is: cat 1.txt Node Name(s) Preparation fragment Partition: Transformation instance: Transformation: Applied rows: Affected rows: Rejected rows: Throughput(Rows/Sec): Throughput(Bytes/Sec): Last... (3 Replies)
Discussion started by: Amey Joshi
3 Replies

10. UNIX for Dummies Questions & Answers

Help needed in search string

Hi , I learning shell scripting.. I need to do the following in my shell script. Search a given logfile for two\more strings. If the the two strings are found. write it to a outputfile if only one of the string is found, write the found string in one output file and other in other... (2 Replies)
Discussion started by: amitrajvarma
2 Replies
Login or Register to Ask a Question