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
# 8  
Old 07-21-2011
Please see output below,

Code:
bash-2.03$ uname -a
SunOS test 5.8 Generic_127721-03 sun4u sparc SUNW,Sun-Fire-15000
bash-2.03$ awk -v d=$DD -v m=$MM '$1==m && $2==d' input_file
awk: syntax error near line 1
awk: bailing out near line 1
bash-2.03$ nawk -v d=$DD -v m=$MM '$1==m && $2==d' input_file
bash-2.03$ cat input_file
 Jul 19 2011                    |    14145673
 Wed Jul 20 10:58:33 EDT 2011   |    14171345
 Wed Jul 20 14:15:16 EDT 2011   |    14156856
 Wed Jul 20 17:26:27 EDT 2011   |    14147096
 Wed Jul 20 23:04:24 EDT 2011   |    14182311
 Wed Jul 20 23:05:10 EDT 2011   |    14182304
 Wed Jul 20 23:42:02 EDT 2011   |    14190892
 Jul 19 2011                    |        NULL
 Wed Jul 20 10:58:33 EDT 2011   |        NULL
 Wed Jul 20 14:15:16 EDT 2011   |        NULL
 Wed Jul 20 17:26:27 EDT 2011   |        NULL
 Wed Jul 20 23:04:24 EDT 2011   |        NULL
 Wed Jul 20 23:05:10 EDT 2011   |        NULL
 Wed Jul 20 23:42:02 EDT 2011   |        NULL

---------- Post updated at 08:45 PM ---------- Previous update was at 08:42 PM ----------

Please see the output below,

Code:
bash-2.03$ uname -a
SunOS test 5.8 Generic_127721-03 sun4u sparc SUNW,Sun-Fire-15000

bash-2.03$ awk -v d=$DD -v m=$MM '$1==m && $2==d' input_file
awk: syntax error near line 1
awk: bailing out near line 1

bash-2.03$ nawk -v d=$DD -v m=$MM '$1==m && $2==d' input_file
 
bash-2.03$ echo $?
0

bash-2.03$ cat input_file
 Jul 19 2011                    |    14145673
 Wed Jul 20 10:58:33 EDT 2011   |    14171345
 Wed Jul 20 14:15:16 EDT 2011   |    14156856
 Wed Jul 20 17:26:27 EDT 2011   |    14147096
 Wed Jul 20 23:04:24 EDT 2011   |    14182311
 Wed Jul 20 23:05:10 EDT 2011   |    14182304
 Wed Jul 20 23:42:02 EDT 2011   |    14190892
 Jul 19 2011                    |        NULL
 Wed Jul 20 10:58:33 EDT 2011   |        NULL
 Wed Jul 20 14:15:16 EDT 2011   |        NULL
 Wed Jul 20 17:26:27 EDT 2011   |        NULL
 Wed Jul 20 23:04:24 EDT 2011   |        NULL
 Wed Jul 20 23:05:10 EDT 2011   |        NULL
 Wed Jul 20 23:42:02 EDT 2011   |        NULL

# 9  
Old 07-21-2011
Oh God,

You changed the order!!!

try

Code:
 
MM="Jul"
DD=20
nawk -v d=$DD -v m=$MM '$2==m && $3==d' input_file

This User Gave Thanks to panyam For This Post:
# 10  
Old 07-21-2011
Sorry ... I just overlooked the filed in my file. Thanks a lot !!! It worked perfectly.

I have used with "||" as well to search for
Code:
"$1 && $2" and "$2 && $3"

Code:
bash-2.03$ nawk -v d=$DD -v m=$MM '$2==m && $3==d || $1==m && $2==d' input_file
 Wed Jul 20 10:58:33 EDT 2011   |    14171345
 Wed Jul 20 14:15:16 EDT 2011   |    14156856
 Wed Jul 20 17:26:27 EDT 2011   |    14147096
 Wed Jul 20 23:04:24 EDT 2011   |    14182311
 Wed Jul 20 23:05:10 EDT 2011   |    14182304
 Wed Jul 20 23:42:02 EDT 2011   |    14190892
 Wed Jul 20 10:58:33 EDT 2011   |        NULL
 Wed Jul 20 14:15:16 EDT 2011   |        NULL
 Wed Jul 20 17:26:27 EDT 2011   |        NULL
 Wed Jul 20 23:04:24 EDT 2011   |        NULL
 Wed Jul 20 23:05:10 EDT 2011   |        NULL
 Wed Jul 20 23:42:02 EDT 2011   |        NULL
 Jul 20 2011                    |       14190892
 Jul  20 2011                   |       14182304

Thanks again for your help.
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