Can anyone guide me with the BASH scripting

Hi,

I am new to BASH script. I am working on the script to parsing the log message . the followings are my task:

  1. Read the multiple log files, such as N1-2009-10-01, N2-2009-10-2, N3-2010-01-02....
  2. Parse the specific message, such as "aaaa"
  3. If found the "aaaa" message in the log file and export to excel file

PS. i use zgrep:

zgrep -a --text "aaaa" * > /home/TTS/myfile

and inside file looks like:

N3.2009-11-26-03-05-02.console.log.tar.gz:2009-11-25 20:12:57,429 - aaaa
N4.2009-11-29-00-25-03.console.log.tar.gz:2009-11-28 10:42:18,209 - aaaa
N6.2009-12-01-10-05-02.console.log.tar.gz:2009-12-01 10:00:24,902 - aaaa

How can i use the script export these information into excel file into three columns, like

N3  2009-11-25 20:12:57 aaaa
N4  2009-11-28 10:42:18 aaaa
N6  2009-12-01 10:00:24 aaaa

Thanks

Try this:

zgrep -a --text "aaaa" * |
awk -F"[ .,]" '{sub(".*:","",$6); sub(",.*","",$7); print $1,$6,$7,$10}'

You could pipe it to Perl as well. Here's the one-liner run on the file -

$
$
$ cat myfile
N3.2009-11-26-03-05-02.console.log.tar.gz:2009-11-25 20:12:57,429 - aaaa
N4.2009-11-29-00-25-03.console.log.tar.gz:2009-11-28 10:42:18,209 - aaaa
N6.2009-12-01-10-05-02.console.log.tar.gz:2009-12-01 10:00:24,902 - aaaa
$
$ # create a "tsv" (tab-separated values) file using Perl
$ perl -lne '/^(.*?)\..*?:(.*?),.*?- (\w+)$/ && print "$1\t$2\t$3"' myfile
N3      2009-11-25 20:12:57     aaaa
N4      2009-11-28 10:42:18     aaaa
N6      2009-12-01 10:00:24     aaaa
$
$

tyler_durden

Thanks.

One more question here.

I tried that the NEW file seems NOT automatically fits in 3 columns in excel. I have to use "text to column" in excel . is there anyway to do this through the script?

If you are talking about the awk script, then try to print a tab-delimited output. I believe a single comma prints a single space in awk.

A tab-delimited text file should open up as an Excel worksheet with column-splitted data without requiring you to do anything else.

tyler_durden

I tried the this , and still not working

zgrep -a --text "aaaa" * | awk -F"[ .,]" '{sub(".:","",$6); sub(",.","",$7); print $1,$6,$7,$10}' > /home/tss/myfile

A comma a 'print' statement prints a value of the of the 'OFS' variable - by default it's a single space.

---------- Post updated at 03:45 PM ---------- Previous update was at 03:42 PM ----------

try this if you need a csv:

zgrep -a --text "aaaa" * | awk -F"[ .,]" '{sub(".*:","",$6);  sub(",.*","",$7); print $1,$6,$7,$10}' OFS=',' > /home/tss/myfile

thanks.

still no luck

---------- Post updated at 03:33 PM ---------- Previous update was at 02:55 PM ----------

just tried and still not working. same result

How about changing that OFS to tab character (\t) instead of comma (,) ?

tyler_durden

Thanks lot!!

It perfectly fits into 4 columns in excel, like

N3      2009-11-25    20:12:57     aaaa
N4      2009-11-28    10:42:18     aaaa
N6      2009-12-01    10:00:24     aaaa

while I would like to display into 3 columns

N3      2009-11-25 20:12:57     aaaa
N4      2009-11-28 10:42:18     aaaa
N6      2009-12-01 10:00:24     aaaa

any thought?

zgrep -a --text "aaaa" * | awk -F"[ .,]" '{sub(".*:","",$6);  sub(",.*","",$7); print $1,$6 " " $7,$10}' OFS='\t' > /home/tss/myfile

Now it works

Thanks a lot to everyone who gives the help