Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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"
share|improve this question

2 Answers 2

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
share|improve this answer
    
Thanks! The sed works for me on OSX. I haven't tried the awk. –  dsg Sep 20 '11 at 17:42
sed 's/[^ ]\+/"&"/g' filename > newname

or, to update the file in-place

sed -i 's/[^ ]\+/"&"/g' filename
share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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