0

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"

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, is there a way to add a trailing new line?
@HTF you are welcome :) to add trailing new line just add a print "\n" in END block. Will edit the answer
1

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'

Comments

1

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'

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.