How to use a string
and another character
as simultaneous delimiters in awk?
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 delimiters at the same time, so that {print $2}
will print 222
as output? Consider input as in the examples above.
You can use simple alternation
$ echo "111:222text333:444" | awk -F "text|:" '{print $2}'
222
What it does
-F "text|:"
sets the field seperator as text
or :
Test
To ensure that this correctly delimits the fields
$echo "111:222text333:444" | awk -F "text|:" '{print $1,$2,$3,$4}'
111 222 333 444
EDIT
If you want to use |
as delimitter, escape the |
as
$ echo "111:222text333:444|hello" | awk -F '\\||text|:' '{print $1,$2,$3,$4,$5}'
111 222 333 444 hello
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.