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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to search multiple pattern in file with grep/sed/awk
# 1  
Old 07-21-2011
Help to search multiple pattern in file with grep/sed/awk

Hello All,

I have a file which is having below type of data,

Code:
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 20" pattern only,

Code:
Jul 20 ---> Single space in between Month & Date
Jul  20 ---> Double space between Month & Date

Also I want to pass the Mon & DD through variables.

Plese advise how o do this.
# 2  
Old 07-21-2011
Code:
 
mo="Jul"
da=20
awk -v d=$da -v m=$mo '$1==m && $2==d' input_file

# 3  
Old 07-21-2011
Hi Thanks for your revert, I tried but this not working.

I wanted to search for below pattern,

Code:
Jul 20 ---> Single space in between Month & Date
Jul  20 ---> Double space between Month & Date

# 4  
Old 07-21-2011
???
Code:
month=Jul
date=20
grep "$month $date"

OR with 2 spaces:
Code:
grep "$month  $date"

# 5  
Old 07-21-2011
If the below does not work, post the required output format.
Code:
VAR="Jul"
VAR2="20"
sed '/'"${VAR}"' *'"${VAR2}"'/!d' inputfile


Last edited by michaelrozar17; 07-22-2011 at 04:25 AM.. Reason: modified sed command as per new input file posted
# 6  
Old 07-21-2011
There is no output, please see below,

Code:
bash-2.03$ VAR="Jul"
bash-2.03$ VAR2="20"
bash-2.03$ sed '/^'"${VAR}"' *'"${VAR2}"'/!d' test.txt
bash-2.03$ echo $?
0

Please advise.
# 7  
Old 07-21-2011
Code:
 
avalon:/disk3/upat/SCRIPTS>cat input_file
Jul 19 2011        |    123456
Jul  19 2011       |    123456
Jul 20 2011        |    123456
Jul               20 2011       |    123456
Jul                                20 2011 | 1111111
avalon:/disk3/upat/SCRIPTS>awk -v d=$d -v m=$m '$1==m && $2==d' input_file
Jul 20 2011        |    123456
Jul               20 2011       |    123456
Jul                                20 2011 | 1111111

Post, the output you are getting and OS version.
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

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

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

4. Shell Programming and Scripting

awk with multiple pattern search

Want to fetch a column with multiple pattern using awk. How to achieve the same. Tried cat test address : 10.63.20.92/24 address : 10.64.22.93/24 address : 10.53.40.91/24 cat test | awk '{print $3}' |awk -F "/" '{print $1}' 10.63.20.92 10.64.22.93 10.53.40.91 Is there any... (2 Replies)
Discussion started by: Manasa Pradeep
2 Replies

5. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

6. UNIX for Dummies Questions & Answers

One liner pattern search with awk/sed/grep

I have an array containing bunch of characters. I have to check this array for specific character and if "Not Found than" use a goto statement to go to USAGE set options = (A B C D E F) @ i = 0 while ($i <= ${#options}) if ($options != "F" || $options != "D") then goto USAGE endif @... (1 Reply)
Discussion started by: dixits
1 Replies

7. Shell Programming and Scripting

awk delete/remove rest of line on multiple search pattern

Need to remove rest of line after the equals sign on search pattern from the searchfile. Can anybody help. Couldn't find any similar example in the forum: infile: 64_1535: Delm. = 86 var, aaga 64_1535: Fran. = 57 ex. ccc 64_1639: Feb. = 26 (link). def 64_1817: mar. = 3/4. drz ... (7 Replies)
Discussion started by: sdf
7 Replies

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

9. Shell Programming and Scripting

Multiline pattern search using sed or awk

Hi friends, Could you please help me to resolve the below issue. Input file :- <Node> <username>abc</username> <password>ABC</password> <Node> <Node> <username>xyz</username> <password>XYZ</password> <Node> <Node> <username>mnp</username> ... (3 Replies)
Discussion started by: haiksuresh
3 Replies

10. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies
Login or Register to Ask a Question