Reputation: 429
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
Upvotes: 0
Views: 341
Reputation: 2819
mawk NF=NF OFS='' FS='^.+ received from '
system 1
system 2
Upvotes: -2
Reputation: 780724
FS
can be a string or regular expression in awk
.
awk -F' received from ' 'NF > 1 {print $2}' mylogfile.log
You could also use sed
to delete everything up to received from
:
sed -n 's/.*received from //p' mylogfile.log
Upvotes: 2
Reputation: 163207
You could set the field separator to received from
and then print the second field if it is there:
awk -F' received from ' 'NF > 1 {print $2}' mylogfile.log
Or using split and check if there is a second field:
awk '{
n = split($0,a," received from ")
if (n > 1) print a[2]
}' mylogfile.log
Output
system 1
system 2
Upvotes: 0
Reputation: 36370
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)
Upvotes: 1