How to using multiple delimiters in awk and sed

When we deal with daily development processing logs, we often encounter the use of delimiters to separate fields.

Due to the inconsistent log format, sometimes there are cases where multiple delimiters are used?

So how to separate fields based on multiple delimiters?

The answer is awk command and sed command.

The log file format is as follows:

➜ cat test.log

#desired result

12 hello
01 ggrep
99 awk
15 hel

Using awk command

➜ awk -F'[:=|]' '{print $1, $2, $3}' test.log

The -F fs option defines the input field separator to be the regular expression fs.

Using sed command

➜ sed 's/[:=|]/ /g' test.log

Related post:

Linux common commands tutorial and use examples
GNU sed syntax and sed examples
awk command tutorial in linux/unix with examples and use cases
Awk tutorial: awk merge files base on the keyword
AWK tutorial: awk custom function and awk example
Awk tutorial: find and kill process use awk
Awk tutorial: awk -F and awk BEGIN{ FS … }
Awk tutorial: awk regex example
Awk tutorial: awk loop example: awk for and while
awk if else, if else if else and awk if else nested example
Awk tutorial: awk NR FNR difference
Awk tutorial: awk sort uniq
Awk tutorial: split on character awk or cut
Awk tutorial: awk newline character
Awk tutorial: three ways of awk execution
How to print tree directory structure in linux/unix
Linux shell batch move files, delete spaces, rename files

Add a Comment

Your email address will not be published. Required fields are marked *