How to find out which delimiter occurred first using a single line awk
.
Suppose I have a file with the contents:
AB BC DE
BC DE AB
DE BC AB
And I want to know which of the three DE
, AB
, BC
occurred first in each line.
I thought I could use a separator BC
, then take its first field, and then BC
, and then take the first field AB
.
It can be done:
$ awk -F'AB' '{print $1}' <file> \
| awk -F'BC' '{print $1}' <file> \
| awk -F'DE' '{print $1}' <file>
However, is there any other way that I can dynamically change the delimiter inside the awk string and get the above thing using awk only once?
Edit: Fixed bugs made earlier.
source
share