Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .