awk regex- include text

Hi

I am trying to filter some data using awk. I have a statement-

 awk 'BEGIN { FS = "\n" ; RS = "" } { if ( $6 = "City: " ) { print "City: Unknown" } else { print $6 } }'`

The $6 values are

City: London
City: Madrid
City: 
City: Tokyo

This expression seems to catch all the lines as satisfying the if statement, but I want it to catch only the City: line. How do I put a regex for that here?

Thanks!

Hi.

You're using an assignment (if ( $6 = "City: " )) instead of a comparison (if ( $6 == "City: " )). Depending on your actual input, you might want if ( $6 ~ /City: / ) - using a regex - instead.

Hi
I am so sorry, what I am using is

awk 'BEGIN { FS = "\n" ; RS = "" } { if ( $6 ~ /City: / ) { print "City: Unknown" } else { print $6 } }'

, I pasted the wrong line before.

Still this regex matches City: London and other similar entries. I want it to match only

City: 

Yes, I think I misread your requirement. if( $6 == "City: " ) should do, or if( $1 ~ /City: *$/ ) is a bit more flexible.

The second one is just perfect :slight_smile: I seem to have badly formatted input !~ /City: / also seems to do the trick.

Thanks a lot.