Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conversion to csv file

Status
Not open for further replies.

CHARLYROSARIO

Programmer
Jun 25, 2007
3
PA
I am working in a ksh script to get one an output csv file of ONLY one line like:
/,45,/usr,56,/var,41

The input file has the following information structure
/,45
/usr,56
/var,41

I was using the following command but it is showing all info overlapped and not separated by comma:

cat dftmp2 | awk '{ for (i=1; i <= NF; i ++) {if (i < NF) printf "%s,",$i;else printf "%s\n",$i}}'`

,/var,41

Any help
 
And what about this ?
Code:
awk '{x=x","$0}END{print substr(x,2)}' dftmp2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
A more cryptic solution using sed :
Code:
sed -n 'H;${x;s/\n//g;s/,$//;p;}'dftmp2
For a production script, prefer the PHV'solution that is more simple to understand and to maintain.

Jean-Pierre.
 
Jean-Pierre, in a legacy unix system the following works better:
Code:
sed -n 'H;${x;s/\n/,/g;s/^,//;s/,$//;p;}' dftmp2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
A shorter variation Chapter11 (UUOC patrol again ;-)):
xargs < dftmp2 | tr ' ' ','

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks a lot to all solutions i have tested using a flat txt file and it worked it.
Now the new problem is the following:
As input file contains <CTRL> Characters at the end of the file, then the file manipulation with all these solutions canot be made:
65,/^M
18,/stand^M
0,/proc^M
0,/dev/fd^M
0,/dev/_tcp^M
0,/system/processor^M
36,/home^M

Is there any way to extract the ^M at the end of each file?
If anybody ask, the only reason why we have such ^M characters is that the info is being transferred by remote shell and unfortunately it is the only one to get it.

Thanks a lot in advance
 
Does your system include dos2unix? This will strip the ^Ms from your file before processing.

I want to be good, is that not enough?
 
Depending on your flavour of Unix, have a look at commands dos2unix or dos2ux.
(^M is needed for Dos, but not for Unix.)
 
Thanks a lot to Ken and hoinz for your tip. It worked great.
Thanks a lot to PHV, aigles and Chapter11 for your help on the csv format.
Best Regards

Charly
 
dos2unix is a crutch ;)

stealing from PHV:

Code:
xargs < dftmp2 | tr ' ' ',' | tr -d '\r'

Or in general, pipe your source file through a
Code:
tr -d '\r'
to strip carriage returns.
 
A no pipe way:
awk '{x=x","$0}END{gsub(/\r/,"",x);print substr(x,2)}' dftmp2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top