heming621
heming621

Reputation: 3

how awk print the regex it maps?

Here is the file contents:

# cat text 
16:10:29 DEBUG MY_Output:90 1 5de0d275c2f55: send response

As I do regex map with awk:

# cat text | awk '{if($0~/[0-9]{2}:[0-9]{2}:[0-9]{2}.*/) print $0}'
(print nothing)

# cat text | awk '/[0-9]{2}:[0-9]{2}:[0-9]{2}.*/ {print $0}'
(print nothing)

# cat text | awk '/[0-9]{2}:[0-9]{2}:[0-9]{2}.*/1'
16:10:29 DEBUG MY_Output:90 1 5de0d275c2f55: send response

My Question is: why {if($0~/[0-9]{2}:[0-9]{2}:[0-9]{2}.*/) print $0} , /[0-9]{2}:[0-9]{2}:[0-9]{2}.*/ {print $0} print nothing, but /[0-9]{2}:[0-9]{2}:[0-9]{2}.*/1 print the result.

As I expected, the three expressions play the same meaning, as [1] describes, 1, {print}, {print $0} do the same thing in action.

Also the another experiment likely to check:

# awk '{if(/[0-9]{2}:[0-9]{2}:[0-9]{2}/) print $0}' <<< "16:10:29" 
(print nothing)

# awk '/[0-9]{2}:[0-9]{2}:[0-9]{2}/{print $0}' <<< "16:10:29"
(print nothing)

# awk '/[0-9]{2}:[0-9]{2}:[0-9]{2}/1' <<< "16:10:29"
16:10:29

Thank you very much.

Upvotes: 0

Views: 117

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133438

After knowing OP's awk version(in comments section) looks like it it OLD one so [0-9]{2} is NOT supported in it(I believe so), so can you try following command once.

awk '/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/' Input_file

This should work with your provided awk version.



Also with old version of awk you could use --re-interval to get it worked, I don't have that version with me so couldn't test it.

awk --re-interval '/[0-9]{2}:[0-9]{2}:[0-9]{2}/'  Input_file

In older versions of awk to invoke EREs we need to include --re-interval with awk codes, hence it is NOT working with normal awk code.

NOTE: In new versions of gawks --re-interval is depreciated since OP has old version of awk so have mentioned it in solution. Adding cross site reference link https://unix.stackexchange.com/questions/354553/awk-repetition-n-is-not-working as per OP's comments too here.

Upvotes: 1

Related Questions