LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris and OpenSolaris.
General Sun, SunOS and Sparc related questions also go here.

Notices



Reply
 
Search this Thread
Old 03-13-2012, 11:36 AM   #1
nano2
Member
 
Registered: May 2007
Posts: 100

Rep: Reputation: 15
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 ?
 
Old 03-13-2012, 01:32 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
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?
 
Old 03-15-2012, 10:35 AM   #3
nano2
Member
 
Registered: May 2007
Posts: 100

Original Poster
Rep: Reputation: 15
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,
 
Old 03-15-2012, 12:17 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.

Last edited by colucix; 03-15-2012 at 02:45 PM.
 
Old 03-27-2012, 04:04 AM   #5
nano2
Member
 
Registered: May 2007
Posts: 100

Original Poster
Rep: Reputation: 15
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 ?
 
Old 03-27-2012, 04:55 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
The script colucix suggested should be interpreted by awk. The error messages you get strongly suggest a shell is trying to interpret it.
 
Old 03-27-2012, 07:46 AM   #7
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
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.
 
Old 03-27-2012, 08:04 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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).
 
  


Reply

Tags
bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
output pid's from ps to a comma delimited file machielr Linux - General 11 04-08-2010 04:33 AM
Writing Screen output to a file and emailing vijayrc Linux - Newbie 7 06-08-2009 11:54 PM
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 06:50 PM
writing output of a program to a file only when all of it is ready pgb205 Linux - Software 4 12-10-2008 03:01 PM
writing output to file Hockeyfan Linux - General 3 10-24-2005 09:02 PM


All times are GMT -5. The time now is 11:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration