I'm trying to gather data directly following a keyword in a file but I have no guarantee where it will appear in said file so I can't use cut or anything else that assumes it will be located at a certain position. If anyone has any suggestions I'd be grateful. Here is an example of the line:
MODStandard , Building=3A 1234, Syntax ID=3B=
In this example I want to search for the keyword "Building" and then grab the four characters "1234" that appear to the right of it.
awk splits up the input line into tokens $1, $2, $3, ... etc.; so for this line
MODStandard , Building=3A 1234, Syntax ID=3B=
$4 equals the string "1234,".
awk's "sub" function performs a replacement on a target string. So
sub(/,/,"",$4);
replaces the comma (in the regex pattern /,/) by zero-length string (2nd argument "") in the target string $4 (which is "1234,"). Thus $4 turns into "1234" which is then printed.
The construct
/Building/ {<action>}
performs the action <action> only for lines that have the literal "Building" in them.