开发者

Printing lines from a file where a specific field does not start with something

开发者 https://www.devze.com 2023-01-16 11:26 出处:网络
I want to print all the lines where 3rd field (fields separated by : ) DO NOT start with # (to signify that 3rd field is a comment). Please note that there may be space(s) between : and #.

I want to print all the lines where 3rd field (fields separated by : ) DO NOT start with # (to signify that 3rd field is a comment). Please note that there may be space(s) between : and #.

Example Input:

A:B:#hdfghdfg

A:B: #dfdfdfg

A:B:C

Desired outpu开发者_运维问答t:

A:B:C

I tried:

awk -F : '$3 ~ /^#/ { print }' run_out5 > run_out6

but it is not working

Thanks,

Jagrati


The regex could be a tiny bit cleaner:

awk -F: '$3 !~ /^ ?#/ { print }'

It's often better to expect repeated whitespace (space or tab) rather than a single space character, which can look identical in printed output.

awk -F: '$3 !~ /^[[:space:]]*#/ { print }'


Use !~ to select lines that do not match the regexp.
Adjust the regexp so that it will match fields with leading spaces.

awk -F : '$3 !~ /^ *#/ {print}'
0

精彩评论

暂无评论...
验证码 换一张
取 消