awk expressions

when I use

Input command | awk '$2~/[0-3]:[3]:[0-3]/'
300 12:3:0  FC    15 normal    559104    525312 6:6:1* 7:6:1           600
301 12:3:1  FC    15 normal    559104    525312 6:6:1  7:6:1*          600
302 12:3:2  FC    15 normal    559104    524288 6:6:1* 7:6:1           600
303 12:3:3  FC    15 normal    559104    524288 6:6:1  7:6:1*          600
324 13:3:0  FC    15 normal    559104    525312 6:6:2* 7:6:2           600
325 13:3:1  FC    15 normal    559104    525312 6:6:2  7:6:2*          600
326 13:3:2  FC    15 normal    559104    524288 6:6:2* 7:6:2           600
327 13:3:3  FC    15 normal    559104    524288 6:6:2  7:6:2*          600
408 10:3:0  FC    15 normal    559104    224256 4:6:3* 5:6:3           600
409 10:3:1  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
410 10:3:2  FC    15 normal    559104    225280 4:6:3* 5:6:3           600
411 10:3:3  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
420 11:3:0  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
421 11:3:1  FC    15 normal    559104    224256 4:6:4  5:6:4*          600
422 11:3:2  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
423 11:3:3  FC    15 normal    559104    225280 4:6:4  5:6:4*          600

Here I don't want (12,13,10,11), and if I write

awk '$2~/[0-12]:[3]:[0-3]/'

where I want first bit to be 0 all the way to 12, I don't get correct results.

Output

input command| awk '$2~/[0-12]:[3]:[0-3]/'
300 12:3:0  FC    15 normal    559104    525312 6:6:1* 7:6:1           600
301 12:3:1  FC    15 normal    559104    525312 6:6:1  7:6:1*          600
302 12:3:2  FC    15 normal    559104    524288 6:6:1* 7:6:1           600
303 12:3:3  FC    15 normal    559104    524288 6:6:1  7:6:1*          600
408 10:3:0  FC    15 normal    559104    224256 4:6:3* 5:6:3           600
409 10:3:1  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
410 10:3:2  FC    15 normal    559104    225280 4:6:3* 5:6:3           600
411 10:3:3  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
420 11:3:0  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
421 11:3:1  FC    15 normal    559104    224256 4:6:4  5:6:4*          600
422 11:3:2  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
423 11:3:3  FC    15 normal    559104    225280 4:6:4  5:6:4*          600
awk '$2~/^(1[0-2]|[0-9]):3:[0-3]/'

Hello vishalgoyal,

Following may help you, Lets say our input file is as follows( I have added few testing lines in it for testing purpose, If I have understood request properly.)
Input_file:

cat file123
300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
300 12:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
301 12:3:1 FC 15 normal 559104 525312 6:6:1 7:6:1* 600
302 12:3:2 FC 15 normal 559104 524288 6:6:1* 7:6:1 600
303 12:3:3 FC 15 normal 559104 524288 6:6:1 7:6:1* 600
324 13:3:0 FC 15 normal 559104 525312 6:6:2* 7:6:2 600
325 13:3:1 FC 15 normal 559104 525312 6:6:2 7:6:2* 600
326 13:3:2 FC 15 normal 559104 524288 6:6:2* 7:6:2 600
327 13:3:3 FC 15 normal 559104 524288 6:6:2 7:6:2* 600
408 10:3:0 FC 15 normal 559104 224256 4:6:3* 5:6:3 600
409 10:3:1 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
410 10:3:2 FC 15 normal 559104 225280 4:6:3* 5:6:3 600
411 10:3:3 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
420 11:3:0 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
421 11:3:1 FC 15 normal 559104 224256 4:6:4 5:6:4* 600
422 11:3:2 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
423 11:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
455 04:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
344 06:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600

Code is as follows:

awk '{if($2 ~ /[0-1][0-9]:[3]:[0-3]/){print $0}}' file123

Output will be as follows.

300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
300 12:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
301 12:3:1 FC 15 normal 559104 525312 6:6:1 7:6:1* 600
302 12:3:2 FC 15 normal 559104 524288 6:6:1* 7:6:1 600
303 12:3:3 FC 15 normal 559104 524288 6:6:1 7:6:1* 600
324 13:3:0 FC 15 normal 559104 525312 6:6:2* 7:6:2 600
325 13:3:1 FC 15 normal 559104 525312 6:6:2 7:6:2* 600
326 13:3:2 FC 15 normal 559104 524288 6:6:2* 7:6:2 600
327 13:3:3 FC 15 normal 559104 524288 6:6:2 7:6:2* 600
408 10:3:0 FC 15 normal 559104 224256 4:6:3* 5:6:3 600
409 10:3:1 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
410 10:3:2 FC 15 normal 559104 225280 4:6:3* 5:6:3 600
411 10:3:3 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
420 11:3:0 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
421 11:3:1 FC 15 normal 559104 224256 4:6:4 5:6:4* 600
422 11:3:2 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
423 11:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
455 04:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
344 06:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600

As we can see in 2nd field's first 2 numbers it is catching the digits now from 00-12 , please let me know you have any queries.

EDIT: Also to get 2nd field's first 2 digits as 0-3 following can help.

awk '{if($2 ~ /[0][0-3]:[3]:[0-3]/){print $0}}' file123

Output will be as follows.

300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600

Thanks,
R. Singh

1 Like

Post a sample of the input and output...

try it prints line if hour is in between 00-09 or 14-23

awk  '$2 ~ /(0[0-9]|1[4-9]|2[0-3]):3:[0-9]/' file

Maybe,

^(1[0-2]|0[0-9]):3:[0-3]

Hi R.Singh!

What is the purpose of using variable i and incrementing it ? I think thread poster not asked for count am I right ? even if suppose had a need to count matches then still it fails since variable i will reset as soon as awk reads next line. Hope you don't mind my comments / compliments

1 Like

Hi Akshay,

My bad that was wrongly copied, I have edited the post now. I was editing the post for same seems you have found out and told same,
you are really fast in replying posts. :b:

I wouldn't mind your comments as it is good for us to learn from our mistakes, learning from seniors(like you and other all techy people) here is always good

Thanks,
R. Singh

Yeah, sometimes not always :slight_smile:

hello Ravinder and all,

Thanks for response.
Is it possible that in second column, if we see as 3 bits, in first bit I can have output for 0-3 and 11, and lets disregard other bits.
I am sure you can make that happen :slight_smile:

Hi Vishalgoyal,

Could you please try following, it may help.
Lets say we have following input file.

cat file421
300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
300 12:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
301 12:3:1 FC 15 normal 559104 525312 6:6:1 7:6:1* 600
302 12:3:2 FC 15 normal 559104 524288 6:6:1* 7:6:1 600
303 12:3:3 FC 15 normal 559104 524288 6:6:1 7:6:1* 600
324 13:3:0 FC 15 normal 559104 525312 6:6:2* 7:6:2 600
325 13:3:1 FC 15 normal 559104 525312 6:6:2 7:6:2* 600
326 13:3:2 FC 15 normal 559104 524288 6:6:2* 7:6:2 600
327 13:3:3 FC 15 normal 559104 524288 6:6:2 7:6:2* 600
408 10:3:0 FC 15 normal 559104 224256 4:6:3* 5:6:3 600
409 10:3:1 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
410 10:3:2 FC 15 normal 559104 225280 4:6:3* 5:6:3 600
411 10:3:3 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
420 11:3:0 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
421 11:3:1 FC 15 normal 559104 224256 4:6:4 5:6:4* 600
422 11:3:2 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
423 11:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
455 04:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
344 06:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
355 011:4:4 FC 15 normal 559104 225280 4:6:4 5:6:4* 600

Code is as follows.

awk '{if($2 ~ /[0][0-3]|[0][1][1]:[3]:[0-3]/){print $0}}' file421

Output will be as follows.

300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
355 011:4:4 FC 15 normal 434424 234242 eqrjehfj2kh2hg2

Thanks,
R. Singh

It's much easier in this case to use a regexp as the Field Separator ( FS)
see this code:

awk  -F'[ :]' '$2 < 10 || $2 > 13' file123 

and that's all

Jean-Paul

Or alternatively to blastit.fr's FS suggestion, force $2 it into numerical context by adding 0 to it:

awk '$2+0<10 || $2+0>13' file

--
with regex:

awk '$2 !~ /^1[0123]/' file

Assuming there are always max 2 digits before the first semicolon in field 2.

Otherwise:

awk '$2 !~ /^1[0123]:/' file