iLuPa
iLuPa

Reputation: 151

Echoing text before each line after grep and cut on Unix

I'm trying to make the output of a job of several log files a little bit more user-friendly on Unix.

The log files go from Web.log being the newest up to Web.log.30 being the oldest. I'm grepping job123 from these files and then piping to a new grep where it takes only the lines with the Exit message, meaning the job has completed. Then I cut it on the comma, so that I only print out the timestamps instead of the entire line. Here's my script:

for file in `ls -tr Web.log*`; do grep job123 $file | grep Exit | cut -d "," -f1; done

Which outputs me the timestamps that I want, in a chronological order:

2015-06-17 21:07:00
2015-06-17 22:07:00
2015-06-17 23:07:00
2015-06-18 00:07:00
2015-06-18 01:07:00
2015-06-18 02:07:00
2015-06-18 03:07:00

All I want to do is add an echo to the beginning of each line saying "Script job123 ran at: " so it would look like this:

Script job123 ran at: 2015-06-17 21:07:00
Script job123 ran at: 2015-06-17 22:07:00
Script job123 ran at: 2015-06-17 23:07:00
Script job123 ran at: 2015-06-18 00:07:00
Script job123 ran at: 2015-06-18 01:07:00
Script job123 ran at: 2015-06-18 02:07:00
Script job123 ran at: 2015-06-18 03:07:00

As the user barely knows Unix, I just need to make it as user friendly as possible to read. I'm struggling with the echoing part.

Also each of the log files is about 25MB long, so the series of pipes can be memory consuming, thus if anyone has any suggestion on speeding things up, it's much appreciated.

Note that this is on a production server, so installing other scripts like multigrep is out of question. I'm tring to do this as a single line script to make the usage more simple to the user.

Any way I can echo that text on the script that I'm currently using? I've already tried echoing it in several stages but with no luck.

thank you.

Upvotes: 2

Views: 1616

Answers (2)

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

Re-using your code, this can be achieved by replacing the cut command (cut -d "," -f1)with awk -F, '{print "Script job123 ran at: " $1}'

Else the same can be achieved with awk alone:

for file in `ls -tr Web.log*`; do awk -F, '/job123/&&/Exit/{print "Script job123 ran at: " $1}' $file; done

Upvotes: 1

P.P
P.P

Reputation: 121417

Store your command's output in a variable and print with additional text:

for file in `ls -tr Web.log*`; do 
  out=$(grep job123 $file | grep Exit | cut -d "," -f1;)
  echo "Script job123 ran at: ${out}"
done

You can also combine the two grep's into one:

out=$(grep -E 'job123.*Exit|Exit.*job123' $file)

If you can use awk then it could simplified to single command:

out=$(awk -F, '/job123/&&/Exit/{print $1}' $file)

Since you'll have to parse the each line, awk solution (without pipes) is going be better. But I can't say how much you speedup you'd get.

Upvotes: 1

Related Questions