sed / awk / grep to extract information from log

Hi all, I have a query that runs that outputs data in the following format -

01/09/12 11:43:40,ADMIN,4,77,Application Group Load: Name(TESTED) LoadId(5137-1-0-1XX-15343-15343) File(/dir/dir/File.T03.CI2.RYR.2012009.11433350806.ARD) InputSize(5344) OutputSize(1359) Rows(2) Time(1.9960) Appl(C99I2),T,1437884,

There are several thousand entries like this going back years in my logs. I need to extract the LoadId Field, which might physically be in a different location because ADMIN could change, the TESTED field could change, etc. But, LoadId will always be-

LoadId(nnnn-n-n-1FAA-NNNNN-NNNNN)

Last two parts might vary.

What is the most efficient way to run through this with the korn shell. I am no expert in any of this. Usually, I'd dump this data into excel and do text to columns, but I need an automated way.

Help is appreciated

$ grep -o "LoadId([^)]*)" file
LoadId(5137-1-0-1XX-15343-15343)

Or with sed

$ sed -n "/LoadId/{s/.*\(LoadId([^)]*)\).*/\1/;p;}" file
LoadId(5137-1-0-1XX-15343-15343)
1 Like

A short sed (but maybe slower if there are many lines without LoadId)

sed -n 's/.*\(LoadId([^)]*)\).*/\1/p' file

The /p modifier prints if a substitution was done.

Hello Scott,

The grep did not work for some reason, though I am going to investigate for future uses.

the sed worked perfectly.

Hi.

-o is a GNU Grep option, so it should work on Linux, or if you're using Solaris, you could try /usr/sfw/bin/ggrep (or /usr/xpg4/bin/grep (it's a while since I've used Solaris, so I don't remember which of those locations its in!)), but the sed will work regardless.

Cheers.

Wondering how I can take this a bit further.

I am looking to get only the information in the ( ) after load ID. So I would like to get 5137-1-0-1XX-15343-15343 by itself.

Just move the part you don't want outside of the remembered expression.

e.g.

$ sed -n "/LoadId/{s/.*LoadId(\([^)]*\).*/\1/;p;}" file
5137-1-0-1XX-15343-15343

A different alternative

perl -nle '/LoadId.(.{24})/ and print $1' jeffs42885.file

Output:

5137-1-0-1XX-15343-15343

Or

sed -n 's/.*LoadId(\([^)]*\)).*/\1/p' file