awk to find pattern in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to find pattern in file
# 1  
Old 04-20-2018
awk to find pattern in file

Hi
I am using awk to find the pattern from the file, but it is not displaying anything. Probably I am missing some syntax in expression.

FILE
Code:
xxxx.Merge.exchMon.BY.qTime          120                 read
xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read

I am using
Code:
CODE=BY
cat $LOCK_FILE | awk '/Merge.exchMon.$CODE.qStart/ && /Merge.exchMon.$CODE.qStop/ && /Merge.exchMon.$CODE.qTime/ && /DelayTracker.$CODE.tStart/ && /DelayTracker.$CODE.tStop1/'

Because qTime is in the first place, but I want to display qTime at the 3rd place so I tried above awk and tried searching pattern with && at 3rd place.

Last edited by sdosanjh; 04-20-2018 at 01:35 PM..
# 2  
Old 04-20-2018
something along these lines?
Code:
CODE=BY
awk -v code="${CODE}"  -F. '$3==code' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-20-2018
Sorry, I forgot to mention
I want the output like below after awk

From
Code:
xxxx.Merge.exchMon.BY.qTime          120                 read
xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read

To
Code:
xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.Merge.exchMon.BY.qTime          120                 read
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read

# 4  
Old 04-20-2018
hm.... this was a confusing explanation to begin with.....
Code:
awk -v code="${CODE}"  -F'[. ]' '$4==code && $5=="qTime"{qt=$0;next}$5=="qStop" && qt{print qt;qt==""}1' myFile

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 04-20-2018
Wouldn't a simple sort help?
This User Gave Thanks to RudiC For This Post:
# 6  
Old 04-20-2018
Thanks vgersh99
But the awk is listing out entire file.
I wanted to find the pattern based on the CODE passed as an argument
that is, want the output as

Code:
xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.Merge.exchMon.BY.qTime          120                 read
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read

---------- Post updated at 09:24 AM ---------- Previous update was at 09:22 AM ----------

Thanks RudiC
the sort always using the dictionary sort and showing the below output
Code:
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read
xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.Merge.exchMon.BY.qTime          120                 read

# 7  
Old 04-20-2018
Like so:
Code:
grep -F ".BY." file | sort -t. -k2,2r -k4,4 -k5,5
xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.Merge.exchMon.BY.qTime          120                 read
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read

These 2 Users Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and awk to find pattern and add priffix

Original File Server1|poweredOn|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Need Output Server1|poweredOn|DR|T1|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Conduction to check find the string "SFCHT1" and "SR" and add prefix has... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

2. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

3. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

4. Shell Programming and Scripting

[awk] find pattern, change next two lines

Hi, hope you can help me... It seems like a straightforward problem, but I haven't had any success so far using my basic scripting and awk "skills": I need to find a pattern /VEL/ in an input file that looks like this: 1110SOL OW25489 1.907 7.816 26.338 -0.4365 0.4100 -0.0736 ... (3 Replies)
Discussion started by: origamisven
3 Replies

5. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

6. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

7. Shell Programming and Scripting

awk to find pattern and add lines

My file goes like this: SID_LIST_HOSTNAME_LISTENER_3 = (SID_LIST = (SID_DESC = (SID_NAME = ORA0008) (ORACLE_HOME = /opt/oracle/product/ORA0008) (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib") ) (SID_DESC = (SID_NAME = ORA0007) ... (4 Replies)
Discussion started by: jpsingh
4 Replies

8. Shell Programming and Scripting

awk command to find particular pattern in file.

Hi I am using the following command to look for anything other than "0000" in a comma seperated file on 11th field. Note: I am looking for "0000" including the double quotes. nawk -F"," '$11!='"0000"'{print $11}' file This is showing incorrect result. Help is appreciated (2 Replies)
Discussion started by: pinnacle
2 Replies

9. UNIX for Advanced & Expert Users

awk script, find pattern

I have a .csv file with columns. In column I like to find which rows contain a certain pattern. That is, my column is: CNR CP CSN CTC EF.DB.U CTC.A D.DB Here, I like to do one thing with EF.DB.U and another with all other. So my question is; how should my if... (3 Replies)
Discussion started by: lulle
3 Replies
Login or Register to Ask a Question