I have a text file which has lines consisting of words (separated by spaces) and I want to put quotes around all of the words.

For example:

Lorem ipsum dolor sit amet
consectetur 
adipisicing elitsed do  

eiusmod tempor 
incididunt ut
  labore et dolore magna 
aliqua

Should become:

"Lorem" "ipsum" "dolor" "sit" "amet"
"consectetur"
"adipisicing" "elitsed" "do"

"eiusmod" "tempor"
"incididunt" "ut"
  "labore" "et" "dolore" "magna"
"aliqua"
link|improve this question

feedback

2 Answers

up vote 2 down vote accepted
sed 's/[^ ][^ ]*/"&"/g' filename > newname

or

awk '{for(i=1;i<=NF;i++)printf("\"%s\" ",$i);printf("\n");}' filename > newname
link|improve this answer
Thanks! The sed works for me on OSX. I haven't tried the awk. – dsg Sep 20 '11 at 17:42
feedback
sed 's/[^ ]\+/"&"/g' filename > newname

or, to update the file in-place

sed -i 's/[^ ]\+/"&"/g' filename
link|improve this answer
Neither version works for me. The first version: output file is same as input file (no quotes were added). Second version: I'm getting an "undefined label" error from sed. I'm on OSX Lion, if that affects things. – dsg Sep 20 '11 at 1:38
glenn - nice sed, works fine for me in ubuntu ..... dsg - try [^ ]+ instead of [^ ]\+. – bryan Sep 20 '11 at 2:36
@bryan -- no dice. – dsg Sep 20 '11 at 17:41
OSX is BSD based, that sed command doesn't work the same as GNU. One article I read mentioned you can't escape characters there as you can in GNU, the other mentioned the -i is interpreted slightly different as well. – OldWolf Sep 21 '11 at 1:26
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.