LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (http://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   writing output to a comma separted file (http://www.linuxquestions.org/questions/solaris-opensolaris-20/writing-output-to-a-comma-separted-file-934239/)

nano2 03-13-2012 10:36 AM

writing output to a comma separted file
 
hi ,
Have the following file file.out with one record per line and want to write the the data to a csv file

file.out
========
number=123344444 name=blah address=abc id=rs12;rs13; date=12-03-2012 04:05.12.06 country=Italy
number=123344444 name=blah2 address=abcd id=rs12;rs13; date=12-03-2012 04:05.12.06country=Italy
number=123344444 name=blah3 address=abcd id=rs12;rs13; date=12-03-2012 04:05.12.06country=Italy

Code:

#/bin/bash
file="file.out"
awk -F "=" '/^Name/ {print ""; next} {printf "%s,", $2} ' $file

but this doesn't work anyone know of a better way to convert the above to comma seperated file ?

MensaWater 03-13-2012 12:32 PM

You can convert the equal signs to commas with sed:

sed -e 's/=/,/g' <file>

Can you give an example line of what you're expecting as output? I'm thinking you're wanting to remove what is before the equal sign maybe?

nano2 03-15-2012 09:35 AM

yes , I want the left hand side of equals to be a header
eg .
Code:

Number,              name,            Address,            id ,                date ,                            country

123344444,            blah,              abc  ,                rs 21 ,            12-03-2012 04:05.12.06 ,              Italy,


colucix 03-15-2012 11:17 AM

In your example there is a missing blank space before country= in lines 2 and 3. Adding spaces back, an awk solution can be:
Code:

BEGIN {
  FS = " *[^ ]+="
}

NR == 1 {
  header = $0
  gsub(/=[^=]* /,",",header)
  gsub(/=[^=]+$/,"",header)
  print header
}

{
  printf "%s", $2
  for ( i = 3; i <= NF; i++ )
    printf ",%s", $i
  printf "\n"
}

where FS is defined as a regular expression matching a string of characters other than the blank space followed by the equal sign. Given your (corrected) example, the output would be:
Code:

number,name,address,id,date,country
123344444,blah,abc,rs12;rs13;,12-03-2012 04:05.12.06,Italy
123344444,blah2,abcd,rs12;rs13;,12-03-2012 04:05.12.06,Italy
123344444,blah3,abcd,rs12;rs13;,12-03-2012 04:05.12.06,Italy

Edit: sorry, I didn't notice this is the Solaris forum. Maybe the suggested solution doesn't work on your system, unless you have GNU awk. Need to rethink this one.

nano2 03-27-2012 03:04 AM

I am using GNU Awk 3.1.5 and i am getting the following errors with the above solution


./oFile: line 16: NR: command not found
./oFile: line 17: header: command not found
./oFile: line 18: syntax error near unexpected token `/=[^=]*'
./oFile: line 18: ` gsub(/=[^=]* /,",",header)'
Any ideas ? should i be using a later version of awk ?

jlliagre 03-27-2012 03:55 AM

The script colucix suggested should be interpreted by awk. The error messages you get strongly suggest a shell is trying to interpret it.

MensaWater 03-27-2012 06:46 AM

You need to put what he wrote in a file then you can run that file with "awk -f <file>"

The output you got is because you're never actually invoking awk.

colucix 03-27-2012 07:04 AM

As jlliagre and MensaWater said. In alternative, you can do an executable script by specifying the interpreter in the very first line of the script itself (using the proper sha-bang), e.g.
Code:

#!/usr/bin/gawk -f

< the GNU awk code goes here >

In this case pay attention to the actual path of the gawk executable (check on your system).


All times are GMT -5. The time now is 10:51 PM.