1

I have this file.txt

kespaul   pts/14       2001:778:200:180 Fri May 29 11:33 - 16:40  (00:07)
kespaul   sshd         2001:778:200:180 Fri May 29 16:33 - 16:40  (00:07)
algvai    pts/2        2001:778:200:400 Fri May 29 15:02 - 16:02  (00:00)
algvai    sshd         2001:778:200:400 Fri May 29 16:02 - 16:02  (00:00)

and I want to create a new one if only beginning hour is equal to 16. So my result should be

kespaul   sshd         2001:778:200:180 Fri May 29 16:33 - 16:40  (00:07)
algvai    sshd         2001:778:200:400 Fri May 29 16:02 - 16:02  (00:00)

I only know how to get hour and minutes value using awk

awk -F ' ' '{print $7}' file.txt

so then I get this

11:33
16:33
15:02
16:02

2 Answers 2

4

You can do pattern matching against individual fields like:

awk  '$7~/^16/ {print}' file.txt

That will look at the 7th field and see if it matches the regular expression ^16 which means "starts with 16". Only if it matches that regex, print the line.

3

You can select lines on which you want to do something. In your case: if the field 7 matches 16: can be done with:

~$ awk '$7 ~/16:/ {print}' file1.txt
kespaul   sshd         2001:778:200:180 Fri May 29 16:33 - 16:40  (00:07)
algvai    sshd         2001:778:200:400 Fri May 29 16:02 - 16:02  (00:00)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .