How to know which delimiter has occurred first using single awk
line.
Assume I have a file having contents:
AB BC DE
BC DE AB
DE BC AB
And I want to know which of the three DE
,AB
,BC
has occurred first in each line.
I thought that I could use delimiter BC
then take its first field and then BC
and then take the first field of AB
.
This can be done by:
$ awk -F'AB' '{print $1}' <file> \
| awk -F'BC' '{print $1}' <file> \
| awk -F'DE' '{print $1}' <file>
However, is there any other way in which I can dynamically change delimiter inside awk line and get the above thing done using awk only once?
Edit: Corrected the mistakes done earlier.
If this isn't what you want:
awk 'match($0,/AB|BC|DE/){print substr($0,RSTART,RLENGTH)}' file
then edit your question to clarify your requirements and provide concise, testable sample input and expected output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With