Help with print out all relevant record if match particular pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with print out all relevant record if match particular pattern
# 1  
Old 12-21-2011
Help with print out all relevant record if match particular pattern

Input file:
Code:
data100_content1    420    700    
data101_content1    107    516        
data101_content2    194    773        
data101_content3    195    917        
data104_content2    36    325        
data105_content1    505    605        
data106_content1    291    565    
data106_content2    498    896    
data107_content1    331    641

Desired output file:
Code:
data101_content1    107    516        
data101_content2    194    773        
data101_content3    195    917        
data104_content2    36    325        
data106_content1    291    565    
data106_content2    498    896

As long as the record contains "content2" and it is shared the same "data*", I would like to output them.
The reason of data101_content1 and data101_content3 also print out is because they shared the same "data101" with content2.
Many thanks for any advice.

Last edited by perl_beginner; 12-21-2011 at 02:39 AM..
# 2  
Old 12-21-2011
your output has content1 also ?
# 3  
Old 12-21-2011
Explain your question clearly.
# 4  
Old 12-21-2011
I just edited my post Smilie

---------- Post updated at 01:16 AM ---------- Previous update was at 01:16 AM ----------

Hopefully my question much more clear now.
Thanks Smilie
# 5  
Old 12-21-2011
Based on your input file .. One way to do ..
Code:
$ nawk -F_ '/content2/{print "grep data"substr($1,5,3)" infile"}' infile | sh

# 6  
Old 12-21-2011
Thanks, jayan_jay.
Your awk code worked for my case.
But it will face problem if my input file content "data1010_content1".
It shouldn't be print in the output file.
# 7  
Old 12-21-2011
Just add an underscore would done the trick ..
Code:
$ nawk -F_ '/content2/{print "grep data"substr($1,5,3)"_ infile"}' infile | sh

This result might also vary if your input file changes .. Smilie
This User Gave Thanks to jayan_jay For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with print out record if first and next line follow specific pattern

Input file: pattern1 100 250 US pattern2 50 3050 UK pattern3 100 250 US pattern1 70 1050 UK pattern1 170 450 Mal pattern2 40 750 UK . . Desired Output file: pattern1 100 250 US pattern2 50 3050 UK pattern1 170 450 Mal pattern2... (3 Replies)
Discussion started by: cpp_beginner
3 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

4. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

5. Shell Programming and Scripting

awk to print record not equal specific pattern

how to use "awk" to print any record has pattern not equal ? for example my file has 5 records & I need to get all lines which $1=10 or 20 , $2=10 or 20 and $3 greater than "130302" as it shown : 10 20 1303252348212B030 20 10 1303242348212B030 40 34 1303252348212B030 10 20 ... (14 Replies)
Discussion started by: arm
14 Replies

6. Shell Programming and Scripting

Script to match a pattern and print only the pattern and after that

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ...... .......xyz: abz: ..... I have tried using awk and cut, bu the position of these values keep changing, so I can use awk and split it into columns. ... (14 Replies)
Discussion started by: Serena
14 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

match pattern 1 and print or match pattern 2 and print

hi all basically i have file called rules which contain lines like below /usr/bwmgr/utils/bwmgr em1 -x 735 -name user92 -addr 10.10.201.92 -addrmsk 255.255.255.252 -bwout 1024000 -bwin 2048000 -statsdevice user92 -stats /usr/bwmgr/utils/bwmgr em1 -x 45032 -name user246 -addr 10.10.224.246... (2 Replies)
Discussion started by: sb245
2 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

Grep for a pattern and print entire record

Hi friends, This is my very first post on forum, so kindly excuse if my doubts are found too silly. I am trying to automate a piece of routine work and this is where I am stuck at the moment-I need to grep a particular ID through a file containing many records(which start with <LRECORD> and end... (6 Replies)
Discussion started by: faiz1985
6 Replies
Login or Register to Ask a Question