1748073 Members
4993 Online
108758 Solutions
New Discussion юеВ

awk/sed help

 
SOLVED
Go to solution
wojtek75
Frequent Advisor

awk/sed help

There is a following line in the file.txt:
name: val1 val2 val3 val4 ... valN
I would like to convert it just to:
"val1","val2","val3","val4",...,"valN"

I would like to use awk/sed for that. Any hint how to do it? Thanks in advance.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: awk/sed help

Hi:

# X="val1 val2 val3 val4"

# echo $X|awk '{for (i=1;i"val1","val2","val3","val4"

Regards!

...JRF...
wojtek75
Frequent Advisor

Re: awk/sed help

Brilliant, thanks. And what if I want to have it in '' instead of "":
'val1','val2','val3','val4',...,'valN'
James R. Ferguson
Acclaimed Contributor

Re: awk/sed help

Hi (again):

Well, if you want single quote instead of double ones, since we are doing this at the shell's command line, I'd use:

# echo $X|awk '{for (i=1;i'val1','val2','val3','val4'

Regards!

...JRF...

wojtek75
Frequent Advisor

Re: awk/sed help

Great, thanks!
Bob E Campbell
Honored Contributor

Re: awk/sed help

Since the question was awk/sed, it only seems fair for a procrastinator to add in the sed method:

sed -e "/^name: / {
s/[ ,]\([^ ,]*\)/\"\1\"/g
}" \
-e s/\"\"/\",\"/g
-e "s/^name://"
"val1","val2","val3","val4"