Karel Adams
Karel Adams

Reputation: 305

awk: keep only part of a field that corresponds to a reg.exp

I have a text file, each line corresponding to a database record, and am doing various things on it with awk. All that works very well, except one point: of one field, I wish to keep only the relevant data; and this corresponds to a regular expression. An example of the given data is below, I wish to keep only the parts like 123.75 (they're aviation comms frequencies; should anyone wonder). The second and third line are what the output should look like for all lines. How can I, within awk, discard the unwanted parts from one field ($3 in this case) ?

50.728901147531914, 2.2349750264486374,Auto-info 123.5
50.33727106924861, 2.9927873611450195,123.500
53.2102778,07.9886111,123.500
53.4197222,07.9044444,123.500 Waterkant Start
53.4813889,07.6516667,123.500; Wittmund TWR 118.725
51.8219444,06.2744444,123.350; 123.150; 123.500
51.4291667,07.6436111,123.500
50.9972222,07.3775000,129.950; 123.500; 123.150
52.9566667,07.5569444,123.500 Steinberg Info
51.3027778,07.9791667,123.500
51.4733333,07.6450000,123.450; 123.500

Upvotes: 1

Views: 60

Answers (1)

anubhava
anubhava

Reputation: 784998

Using awk you can do:

awk -F'[;,][[:blank:]]*' -v OFS=, '{
   gsub(/^[^[:digit:]]*|[^[:digit:]]*$/, "", $3)
}
NF>3 {
   gsub(/^[^[:digit:]]*|[^[:digit:]]*$/, "", $4)
} 1' file

50.728901147531914,2.2349750264486374,123.5
50.33727106924861,2.9927873611450195,123.500
53.2102778,07.9886111,123.500
53.4197222,07.9044444,123.500
53.4813889,07.6516667,123.500,118.725
51.8219444,06.2744444,123.350,123.150,123.500
51.4291667,07.6436111,123.500
50.9972222,07.3775000,129.950,123.500,123.150
52.9566667,07.5569444,123.500
51.3027778,07.9791667,123.500
51.4733333,07.6450000,123.450,123.500

Upvotes: 1

Related Questions