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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String

Status
Not open for further replies.

sabetik

IS-IT--Management
Nov 16, 2003
80
GU
is there a command in AWK that I can add "" and , to a text file: here example of the file

12121-54687-25 cap top 25.65
12424-58475 cover cap 35.25

I like to print like this
"12121-54687-25","cap top","25.65"
"12424-58475","cover cap","35.25"

Please help
Kamran


 
Something like this ?
awk '
BEGIN{fmt="\"%s\""}
{printf fmt,$1;for(i=2;i<=NF;++i)printf ","fmt,$i;printf "\n"}
' /path/to/input
Depending of your awk flavor, you may try this:
awk '
{for(i=1;i<=NF;++i)printf "\"%s\""(i<NF?",":"\n"),$i}
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
an oneline-sed:
nota: Tab is a tab-char and Space a space-char

s/[TabSpace]*[TabSpace]/Space/g;s/\(.*\) \(.*\) \(.*\) \(.*\)/"\1","\2 \3","\4"/


:) guggach
 
Oops, reread the question.
Didn't realize the embedded space syndrom.
Seems we have to play with fixed size fields:[tt]
12121-54687-25 cap top 25.65
12424-58475 cover cap 35.25
....+....1....+....2....+....3.[/tt]
With decent awk, you can try this:
awk '
BEGIN{nf=split("1 16 27 32",s)}
{for(i=1;i<nf;++i){
a=substr($0,s,s[i+1]-s);sub(/ *$/,"",a)
printf "\"%s\""(i<(nf-1)?",":"\n"),a
}}
' /path/to/input
With old versions:
awk '
BEGIN{nf=split("1 16 27 32",s);fmt="\"%s\""}
{for(i=1;i<nf;++i){
a=substr($0,s,s[i+1]-s);sub(/ *$/,"",a)}
printf fmt,a[1];for(i=2;i<nf;++i)printf ","fmt,a;printf "\n"}
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top