awk multiple delimiters

Good news! awk field separator can be a regular expression. You just need to use -F"<separator1>|<separator2>|...":

awk -F"/|=" '{print $3, $5, $NF}' file

Returns:

tc0001 tomcat7.1  demo.example.com  tc0001 tomcat7.2  quest.example.com  tc0001 tomcat7.5  www.example.com

Here:

  • -F="/|=" sets the input field separator to either / or =. Then, it sets the output field separator to a tab.

  • {print $3, $5, $NF} prints the 3rd, 5th and last fields based on the input field separator.


See another example:

$ cat file  hello#how_are_you  i#am_very#well_thank#you

This file has two fields separators, # and _. If we want to print the second field regardless of the separator being one or the other, let's make both be separators!

$ awk -F"#|_" '{print $2}' file  how  am

Where the files are numbered as follows:

hello#how_are_you           i#am_very#well_thank#you  ^^^^^ ^^^ ^^^ ^^^           ^ ^^ ^^^^ ^^^^ ^^^^^ ^^^    1    2   3   4            1  2   3    4    5    6

Comments

Popular posts from this blog

Künefe Tarifi

Bir Word Belgesinde Kapak Harici Kaç sayfa Olduğunu Bulmak