1835936 Members
1756 Online
110088 Solutions
New Discussion

Re: Unix Scripting

 
Oktay Tasdemir
Advisor

Unix Scripting

Hello,

I have a scripting question.

I have a unix file named source which contains the following text

Password:
cpi_quarter cpi face_currency
-------------------------- -------------------- -------------
Dec 1 2002 12:00AM 139.500000 aud
Mar 1 2003 12:00AM 141.300000 aud
Jun 1 2003 12:00AM 141.300000 aud
Sep 1 2003 12:00AM 142.100000 aud

(return status = 0)

I would like to reformatted to a CSV file as per the attachment.

Any ideas would be appreciated.

Thanks
Oktya
Let the fun and games begin
4 REPLIES 4
Karthik S S
Honored Contributor

Re: Unix Scripting

Is that what you are looking for?,

cat source | grep -v Password | grep -v cpi_ | grep -v status | sed s/\ /\,/g
--------------------------,--------------------,-------------
Dec,1,2002,12:00AM,139.500000,aud
Mar,1,2003,12:00AM,141.300000,aud
Jun,1,2003,12:00AM,141.300000,aud
Sep,1,2003,12:00AM,142.100000,aud,


-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Oktay Tasdemir
Advisor

Re: Unix Scripting

Hi Karthik,

Thanks for the prompt response, I have attached the file to another message.
Let the fun and games begin
Jeroen Peereboom
Honored Contributor

Re: Unix Scripting

Try this one. Modify the first line to your shell (ksh, posix, sh, ...)

#!/bin/bash
grep -v -e '^Password:' -e '^(return status' -e '^ *$' source | awk '{
if (NR==1) { printf "%s,%s,%s\n", $1, $2, $3}
else
printf "%s %s %s %s,%s,%s\n", $1, $2, $3, $4, $5, $6
fi
}'

In words:
- grep removes lines with patterns mentioned (including empty lines)
- awk prints the first record (NR==1) using format different from the other records.

JP.
Graham Cameron_1
Honored Contributor

Re: Unix Scripting

Can't see any attachment, but it sounds like you can do it all in awk:

cat YOURFILE|awk '
/^\(return status/ {next}
/^Password/ {next}
/^$/ {next}
{ for (i=1;i<=NF;i++)
printf ("%s,", $i)
printf ("\n")
}
'
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.