Command/script to match a field and print the next field of each line in a file.


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 08-10-2015
Command/script to match a field and print the next field of each line in a file.

Hello,

I have a text file in the below format:
Code:
Source                                                                                              Destination                                                                              State          Lag        Status
CQA02W2K12pl:D:\CAQA                                                                            infrabkpsrvpl:/vol/CQA02W2K12srv/CAQA                                                Snapvaulted    13:46:44   Idle
DBIntranetpl:E:\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL                                  infrabkpsrvpl:/vol/DBIntranetsrv/MSSQL10                                                Snapvaulted    13:46:43   Idle

And the file has hundreds of such entries.

What I'm interested in is the lag column- which is always in the format hh:mm:ss. I want to read the file line by line, check if the "lag" column in greater than 48:00:00 (just matching the hour field would be enough) and if the lag is greater than 48:00:00, append that particular line to another file.

How can I achieve this?

I tried awk, but since some of the line descriptions have spaces, $4 is not always lag column.

Thanks,
Pocodot
# 2  
Old 08-10-2015
here's something to start with:
Code:
awk '{print $(NF-1)}' myFile

# 3  
Old 08-10-2015
Thanks! Here's something else i'm trying based on your reply:

Code:
 awk '{if($(NF-1) < 48) print $0}'   OP.txt > output.txt

This command would be perfect for me, but $(NF-1) is in the format hh:mm:ss.

Is there any way I can just compare the hh part ?

Thanks,
Pocodot
# 4  
Old 08-10-2015
Code:
awk  '$(NF-1) ~ /..:..:../ {split($(NF-1), a, ":"); if(a[1] < 48)print} OP.txt > output.txt

This User Gave Thanks to Aia For This Post:
pocodot (08-10-2015)
# 5  
Old 08-10-2015
Assuming there are never any spaces in the Status field, try:
Code:
awk -F'[[:space:]]+|:' '$(NF-3) + 0 >= 48' OP.txt >> output.txt

Note the >> redirection operator instead of >. (You said you wanted to append to the destination file; not replace its current contents.)
# 7  
Old 08-10-2015
I guess it doesn't work on my shell.

Code:
[aia@ludus tmp]$ nl pocodot.file
     1  Source                                                                                              Destination                                                                              State          Lag        Status
     2  CQA02W2K12pl:D:\CAQA                                                                            infrabkpsrvpl:/vol/CQA02W2K12srv/CAQA                                                Snapvaulted    13:46:44   Idle
     3  DBIntranetpl:E:\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL                                  infrabkpsrvpl:/vol/DBIntranetsrv/MSSQL10                                                Snapvaulted    13:46:43   Idle

Code:
[aia@ludus tmp]$ awk -F'[[:space:]]+|:' '$(NF-3) + 0 >= 48' pocodot.file
[aia@ludus tmp]$ echo $?
0


It appears that the logic is broken
Code:
[aia@ludus tmp]$ awk -F'[[:space:]]+|:' '{print $(NF-3) + 0, ">=", 48}' pocodot.file
0 >= 48
13 >= 48
13 >= 48


Last edited by Aia; 08-10-2015 at 11:21 PM.. Reason: Add broken logic as clarification that it is not the shell's problem.
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
awk to print text in field if match and range is met cmccabe Shell Programming and Scripting 6 02-19-2018 08:41 AM
Use strings from nth field from one file to match strings in entire line in another file, awk jvoot UNIX for Beginners Questions & Answers 1 01-16-2018 08:31 AM
Use two field separator in the same line and print them antoniorajr Shell Programming and Scripting 4 08-11-2015 03:49 AM
Match columns and print specific field Pushpraj Shell Programming and Scripting 6 11-07-2014 08:20 AM
print whole line if the 1st field contains... oreka18 Shell Programming and Scripting 1 05-07-2012 01:46 PM
Match pattern in a field, print pattern only instead of the entire field lucasvs UNIX for Dummies Questions & Answers 18 03-05-2012 08:07 PM
AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2 right_coaster Shell Programming and Scripting 4 10-06-2011 06:07 PM
only print line if 3rd field is 01 ajp7701 Shell Programming and Scripting 2 04-05-2011 05:54 PM
How to print line if field matches? stinkefisch Shell Programming and Scripting 7 09-03-2010 09:45 AM
simplify the script, check field match to value in a file jimmy_y Shell Programming and Scripting 6 05-25-2010 03:40 AM
need Shell script for Sort BASED ON FIRST FIELD and PRINT THE WHOLE FILE WITHOUT DUPLICATES tuffEnuff Shell Programming and Scripting 2 03-31-2010 05:11 AM
Script to Match first field of two Files and print indian.ace Shell Programming and Scripting 3 10-16-2009 11:53 AM
Print the entire line if second field has value P ppat7046 Shell Programming and Scripting 4 07-23-2009 09:48 AM
how to print field n of line m Deanne Shell Programming and Scripting 11 01-30-2009 02:41 AM
Print last occurrence if first field match Raynon Shell Programming and Scripting 9 03-13-2008 03:28 AM