awk splits by multiple delimiters|paste

We know that awk can perform operations similar to cut, such as a file data as follows zhc

-123|zhang

hongchangfirst-99|zhang

hongchang-100|zhang




If we

awk -F '-' '{print $1;}' data

will be prints out

zhc

hongchangfirst

hongchang




but what if I want to split based on multiple delimiters? One way is to awk twice, but we can tell awk all our delimiters at once, like - and | these two, like

awk -F '[-|]' '{print $3;}' data

will print out

zhang

zhang

zhang

is that simple, there is one more question, what if we want to use [] as a delimiter? There is a way, just like this:

awk -F '[][]' '{print $3;}' data





Here is one more trick, if you want to put two files on the same line according to the corresponding line, you can use paste, for example:

data1 file is

1

2

3

data2 file is

zhang

zhc

hongchangfirst




then you want to get

1 zhang

2

zhc 3 hongchangfirst




Then you can do this,

paste data1 data2




uses the tab key as the part separator by default, and you can also customize the separator, such as using the = sign:

paste -d'=' data1 data2

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326618503&siteId=291194637
awk