Input Data:
abc 12-25-05 gifts bear horse
xyz new 03-05-64 house 01-01-99 frogs
I want to extract only those fields that have a "-" in them, but I also want them to display on the same line for each record.
example of desired output:
12-25-05
03-05-64 01-01-99
I found this to work:
awk '{for(i=1;i<=NF;++i)if($i !~/-/)$i=""; print}'
...but, because I'm setting non-matching fields to null (i.e. - ""), I'm getting additional spaces in my final output.
How can I either ...
1.)trim the white space using the above AWK cmd, or
2.)do a positive search (i.e. - search for "-", not use the "!") and only print the field(s) that match on 1 line per record. Example:
awk '{for(i=1;i<=NF;++i)if($i ~/-/) print $i}'
This cmd give me the correct fields, but it displays one field per row, which is what I don't want - I want to have all matching fields on same line per record!
abc 12-25-05 gifts bear horse
xyz new 03-05-64 house 01-01-99 frogs
I want to extract only those fields that have a "-" in them, but I also want them to display on the same line for each record.
example of desired output:
12-25-05
03-05-64 01-01-99
I found this to work:
awk '{for(i=1;i<=NF;++i)if($i !~/-/)$i=""; print}'
...but, because I'm setting non-matching fields to null (i.e. - ""), I'm getting additional spaces in my final output.
How can I either ...
1.)trim the white space using the above AWK cmd, or
2.)do a positive search (i.e. - search for "-", not use the "!") and only print the field(s) that match on 1 line per record. Example:
awk '{for(i=1;i<=NF;++i)if($i ~/-/) print $i}'
This cmd give me the correct fields, but it displays one field per row, which is what I don't want - I want to have all matching fields on same line per record!