How to set awk separator to string or character?

How to use and one more as concurrent delimiters in awk? string

character

sdlcb@ubuntu:~/AMD_C/SO$ echo "111:222text333:444" | awk -F ':' '{print $2}'
222text333

sdlcb@ubuntu:~/AMD_C/SO$ echo "111:222text333:444" | awk -F "text" '{print $2}'
333:444

      

So the question is, how can we use " text

" and " :

" as separators at the same time so that it {print $2}

prints 222

as output? Consider the input as in the above examples.

+3


source to share


1 answer


You can use simple alternation

$ echo "111:222text333:444" | awk -F "text|:" '{print $2}'
222

      

What does he do

  • -F "text|:"

    sets the field separator as text

    or:

Test



To make sure that it limits the margins correctly

$echo "111:222text333:444" | awk -F "text|:" '{print $1,$2,$3,$4}'
111 222 333 444

      

EDIT

If you want to use |

as a delimiter, release |

like

$ echo "111:222text333:444|hello" | awk -F '\\||text|:' '{print $1,$2,$3,$4,$5}'
111 222 333 444 hello

      

+3


source







All Articles