Operating System - HP-UX
1834935 Members
2285 Online
110071 Solutions
New Discussion

Manipulating a log with a Script

 
SOLVED
Go to solution

Manipulating a log with a Script

Hi folks!

I am having a problem with a shell script??? since I am new on Shell scripting but right now learning a lot with your help???

I have a log file with this format (MRTG log file format) ;)

1045784114 60 59 60 59
1045783813 63 62 63 62
1045783800 62 61 63 62

Basically, I want to replace the space ??? ??? character with a comma ???,??? character, so I can export this file to a Database and read the contents with this CSV format.

Also, need to add a value in the beginning of each line, so the new file can be read like this:

Hostname,1045784114,60,59,60,59
Hostname,1045783813,63,62,63,62
Hostname,1045783800,62,61,63,62

I tried more, and cat, but cant get this working???
Any help or hints will be really appreciated.

Rogelio
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: Manipulating a log with a Script

tr ' ' ',' < infile > outfile
If it ain't broke, I can fix that.
S.K. Chan
Honored Contributor

Re: Manipulating a log with a Script

Something like this .. ?
$ cat file|sed 's/^/Hostname,/'|sed 's/ /,/g'
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Manipulating a log with a Script

Hi Rogelio

sed -e 's/^/HOSTNAME,/g' -e 's/ /,/g' -e 's/,$//g' infile > outfile

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Pete Randall
Outstanding Contributor

Re: Manipulating a log with a Script

and for the hostname:

sed 's/^/hostname/'

Pete
James R. Ferguson
Acclaimed Contributor

Re: Manipulating a log with a Script

Hi Rogelio:

Here's another way:

# awk -v hostname=$(hostname) 'BEGIN{ OFS=","};{$1=$1;print hostname,$0}' filename

In this case the actual server hostname is injected into the output lines.

Regards!

...JRF...

Re: Manipulating a log with a Script

Thank you all for the answers!!!

Sridhar's command made the whole job in one step, thank you very much.

Best regards.

Rogelio