Home > database >  How to use awk for multiple character delimiter?
How to use awk for multiple character delimiter?

Time:12-09

I have a log file with lines like :

Insert request received from system 1
Modify request received from system 2

I want to get the text after "received from" from the log. The equivalent grep and cut command would be (if it supported multiple character delimiter)

grep "received from" mylogfile.log | cut -d"received from" -f1

How can I recreate this with awk

CodePudding user response:

FS can be a string or regular expression in awk.

awk -F' received from ' '/received from/ {print $2}' mylogfile.log

You could also use sed to delete everything up to received from:

sed -n '/received from/s/.*received from //p' mylogfile.log

CodePudding user response:

You might do it following way, let file.txt content be

Insert request received from system 1
Modify request received from system 2

then

awk 'sub(/.*received from /,"")' file.txt

gives output

system 1
system 2

Explanation: I use sub string function with 2 arguments: regular expression and empty string, so it does alter current line in-place and return 1 if change made else 0, this value is then used for selection - only lines when change was made are printed. Warning: this solution assumes each line has at most 1 received from , if this does not hold true rightmost one is treated as delimiter.

(tested in GNU Awk 5.0.1)

  • Related