awk get search pattern from a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk get search pattern from a file.
# 1  
Old 07-04-2012
awk get search pattern from a file.

Here Is a problem I am facing with awk.

Query --> I want to search for a string in a file and print next 15 lines below the matched string.

1.We do not have GNU grep so cannot use grep -A or grep -B commands.
2. Instead of passing the search pattern as a string to awk. I want the awk to read the pattern from a file.

This is the command which I am using:
Code:
cat fulldatafile.txt | awk 'c-->0;/'`cat IDnumber.txt`'/{c=17}'

So what I did:
cat the full data file (file which contains information associated with ID) and then using the awk command I want to search for the only single ID, the ID is in the IDnumberfile which is updated by an external program sometimes. Kindly, help me in finding out how to read a file to get the pattern.
# 2  
Old 07-04-2012
Quote:
Originally Posted by togotutor
Query --> I want to search for a string in a file and print next 15 lines below the matched string.
With whatever little awk knowledge I have, this is what I could assimilate into a solution:
Code:
awk '/pattern/ {c=0; f=1; print; next} f==1 && c<=15 {print; c++} c==15 {exit}' inputfile

After a bit of googling, I found a solution in this link.
Code:
awk '/pattern/{c=15;{print}next}c-->0' inputfile


Last edited by balajesuri; 07-04-2012 at 06:57 AM..
# 3  
Old 07-04-2012
Try this too -
Code:
awk '/pattern/ {for(i=0;i<15;i++) {print;if(!getline) exit;} }' inputFile

# 4  
Old 07-04-2012
Code:
[ -r IDnumber.txt ] && awk 'FNR==NR{patt=$0;next} index($0,patt){for(i=1;i<=15 && getline;i++) print}' IDnumber.txt fulldatafile.txt

# 5  
Old 07-04-2012
Thank You Guys, let me try these solutions out and I will update you shortly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

3. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

4. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

5. UNIX for Advanced & Expert Users

use of variables in awk to search for pattern from a file

Hi, I need to extract all the content between two strings stored in two variables "startstring" and "endstring" startstring=hello enstring=world #notworking awk '/$startstring/, $NF ~ /$endstring/ ' file > file2 The above code is not working with variables. It works when actual string... (2 Replies)
Discussion started by: jeanjkj
2 Replies

6. Shell Programming and Scripting

Help to search multiple pattern in file with grep/sed/awk

Hello All, I have a file which is having below type of data, Jul 19 2011 | 123456 Jul 19 2011 | 123456 Jul 20 2011 | 123456 Jul 20 2011 | 123456 Here I wanted to grep for date pattern as below, so that it should only grep "Jul 20" OR "Jul ... (9 Replies)
Discussion started by: gr8_usk
9 Replies

7. Shell Programming and Scripting

Awk/Sed: Search Pattern from file and Print

Hi, I want to search for patterns (from a file) in a file and print the line matching the patterns and the line before it. I have to search for 100s of patterns from a file. Any help with AWK or Sed. Thanks! (2 Replies)
Discussion started by: saint2006
2 Replies

8. Shell Programming and Scripting

Awk search for string pattern in delimited file

I've got a semicolon delimited file. I would like to search for fields that match a pattern, and not hardcoded eg "mth". *th=something If the delimited field fulfills this condition, eg. mth=value I would like to print out both key and value for some number comparison. eg. if value > "12"... (5 Replies)
Discussion started by: alienated
5 Replies

9. Shell Programming and Scripting

awk pattern search and put in a file

hi I am doing a task that is i have to search for multiple pattern from a file's 9 th column and then redirect it to a new file awk -F, 'BEGIN {OSF=","} { if ($9 == "OTF") print $0}' test1.tsv > test.tsv and the patterns are OST. ODI,MIN LIKE OTF Bit not getting the desired output... (11 Replies)
Discussion started by: pranabrana
11 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question