Reputation: 7300
I would like to format the output from the log file to a single line using BASH:
Log output:
# egrep "^(Infected|Cleaned)" /var/log/scan.log
Infected: files - 0, objects 0
Cleaned: files - 0, objects 0
I was able to print the results on one line but I'm not sure how I can format it and remove spaces:
# egrep "^(Infected|Cleaned)" /var/log/scan.log | awk '/Infected/{if (x) print x; x="" } { x = (!x) ? $0 : x "; " $0 } END { print x }'
Infected: files - 0, objects 0; Cleaned: files - 0, objects 0
Desired format:
Infected:F-0, O-0; Cleaned:F-0, O-0"
Upvotes: 0
Views: 78
Reputation: 26667
Clean
awk '/Infected|Cleaned/{ORS=";"; print $1 "F-"$4 "O-"$6}' inputfile
will produce output
Infected:F-0,O-0;Cleaned:F-0,O-0;
ORS
output record seperator is set to ;
which seperates the differnt line.
/Infected|Cleaned/
select the lines that match the pattern
EDIT
To add a trailling newline
awk '/Infected|Cleaned/{ORS=";"; print $1 "F-"$4 "O-"$6}END{print "\n"}'
OR
awk '/Infected|Cleaned/{ORS=";"; print $1 "F-"$4 "O-"$6}' sam; echo
Upvotes: 1
Reputation: 56
Try:
awk 'BEGIN { FS = " +"; ORS = " " }; { print $1, "F-", $4, "O-", $6 }' /var/log/scan.log
FS: field separator, set to one or more spaces
ORS: record separator for output, set to a space
With this value of FS, the desired fields are 1, 4, 6; the trailing separators (":", ",", ";", resp.) will be included.
Upvotes: 1
Reputation: 525
I've piped the output to perl one-liner, this should do it
| perl -ne 's/\s+files\s+\-\s+/ F-/g; s/\s+objects\s+/ O-/g; print'
Upvotes: 1
Reputation: 5351
horribly quick and dirty:
egrep "^(Infected|Cleaned)" /var/log/scan.log | awk '/Infected/{if (x) print x; x="" } { x = (!x) ? $0 : x "; " $0 } END { print x }' | sed 's/\s//g' | sed 's/files/F/g' | sed 's/objects/ O-/g' | sed 's/;/; /g'
Upvotes: 1