Issue
My current command:
cat source.txt*|sort -V -k2 -k3 |egrep "WiFiAdapterAndroid|Lat:"
+INFO 2019-12-17 10:10:51,488 [AndroidGPS:-1225032884] Lat: 39.287593; Lon: -94.659815; Speed: 19MPH; Heading: N; Accuracy: 3m; Alt: 281m; SatsUsed: 17; SatsInView: 23.
+INFO 2019-12-17 10:11:20,729 [WiFiAdapterAndroid:-1225032884] netStateChgd: DISCONNECTED
+INFO 2019-12-17 10:11:20,856 [WiFiAdapterAndroid:-1225032884] supStateChgd: COMPLETED->DISCONNECTED
+INFO 2019-12-17 10:11:21,082 [WiFiAdapterAndroid:-1225032884] supStateChgd: DISCONNECTED->SCANNING
I would like the list to show as it is but make it so i only have the actual lat and lon numbers and no other data in that line.
Example of what i want to see:
39.287593 -94.659815
+INFO 2019-12-17 10:11:20,729 [WiFiAdapterAndroid:-1225032884] netStateChgd: DISCONNECTED
+INFO 2019-12-17 10:11:20,856 [WiFiAdapterAndroid:-1225032884] supStateChgd: COMPLETED->DISCONNECTED
+INFO 2019-12-17 10:11:21,082 [WiFiAdapterAndroid:-1225032884] supStateChgd: DISCONNECTED->SCANNING
Solution
Like this?
sort -V -k2 -k3 source.txt* |
awk '/Lat:/ { gsub(/;/, ""); print $6, $8 }
/WiFiAdapterAndroid/'
Notice also how sort
can read a list of files without the help from cat
.
cut
is not suitable here because it can't decide which lines to modify; it's all or nothing. Conversely grep
can't modify the strings it extracts. Awk, then, provides an easy and compact notation for doing both.
Briefly, Awk executes a script on each line of input at a time (or, more broadly, a record; it's easy to configure it to operate on units which are parts of lines or collections of adjacent lines, too). Each script unit consists of a condition and an action; both of them are optional, so an action without a condition matches all lines unconditionally, and a condition without an action defaults to printing the input for which the condition matched.
The first line of the script has a regex condition which selects lines which match the regular expression Lat:
; the action cleans up the line with a simple substitution to remove any semicolons, then prints the sixth and eighth tokens on the line. (Each record is split into fields; again, there is a lot of flexibility here, but by default, each field is a non-whitespace token separated by whitespace from adjacent tokens.) And finally, as you might guess, the second condition is another regex, which causes matching inputs to be printed.
Answered By - tripleee Answer Checked By - Katrina (WPSolving Volunteer)