29

I have the following list of words:

name,id,3

I need to have it double quoted like this:

"name,id,3"

I have tried sed 's/.*/\"&\"/g' and got:

"name,id,3

Which has only one double quote and is missing the closing double quote.

I've also tried awk {print "\""$1"\""} with exactly the same result. I need help.

anubhava
  • 761,203
  • 64
  • 569
  • 643
minerals
  • 1,195
  • 4
  • 15
  • 22
  • 1
    Your sed command works on my computer. You can omit the backslash before the double quotes since they are useless in a single quoted string. – Scharron May 25 '12 at 12:48
  • hmmm, yes, this is weird. I get one double quote all the same. My original file is file.csv -- has a long list of "name,id,[[:num:]]". Why doesn't it work? – minerals May 25 '12 at 12:54
  • 1
    The `g` isn't necessary, but it's not causing your problem. What is your platform? – Dennis Williamson May 25 '12 at 13:30

7 Answers7

28

Use this to pipe your input into:

sed 's/^/"/;s/$/"/'

^ is the anchor for line start and $ the anchor for line end. With the sed line we're replacing the line start and the line end with " and " respectively.

Example:

$ echo -e "name,id,2\nname,id,3\nname,id,4"|sed 's/^/"/;s/$/"/'
"name,id,2"
"name,id,3"
"name,id,4"

without the sed:

$ echo -e "name,id,2\nname,id,3\nname,id,4"
name,id,2
name,id,3
name,id,4

Your file seems to have DOS line endings. Pipe it through dos2unix first.

Proof:

$ cat test.txt
name,id,2
name,id,3
name,id,4
$ sed 's/^/"/;s/$/"/' test.txt
"name,id,2
"name,id,3
"name,id,4
$ cat test.txt|dos2unix|sed 's/^/"/;s/$/"/'
"name,id,2"
"name,id,3"
"name,id,4"
0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
22

Your input file has carriage returns at the end of the lines. You need to use dos2unix on the file to remove them. Or you can do this:

sed 's/\(.*\)\r/"\1"/g'

which will remove the carriage return and add the quotes.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • that worked! you're right, the file comes from windows and was generated in windows – minerals May 25 '12 at 13:47
  • great solution! can you put some light on what \r and "\1" is doing here ? – sherpaurgen Jul 13 '17 at 08:33
  • @satch_boogie: `\r` is a carriage return. `\1` is the contents of the capture done by the parentheses (so in this case, the entire line before the carriage return). The result is that the carriage return is removed and quotes are added. I think the `g` (global) at the end is unnecessary, but my answer preserved it from the OP's question. See [this](http://www.regular-expressions.info/brackets.html) for more information about capturing groups. – Dennis Williamson Jul 13 '17 at 10:15
11

You can use awk instead of sed like this:

line='name,id,3'
echo $line | awk '{printf("\"%s\"\n", $0);}'

OR even better is to use BASH printf like this:

printf '"%s"\n' $line
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I have command as this, but i need each output line to be enclosed within " ". How to modify this command? :awk '{ for (i = 2; i <= NF; i++) { printf("%s ", $i); } printf("\n") }' all.txt > result.txt – user3823859 Apr 29 '15 at 06:59
  • To quote each line just use: `awk '{printf("\"%s\"\n", $0)}' file` – anubhava Apr 29 '15 at 12:26
7

Simplest way I've found is to use the HEX codes for double quotes:

 echo name,id,3 | awk '{print "\x22" $1 "\x22"}'
 "name,id,3"
soulBleach
  • 71
  • 1
  • 2
3

Similar to @Dennis solution,

Use

sed 's/\(.*\)/"\1"/g'

No need to look for '\r', This will apply double quote for all strings. This does not require run dos2unix on the input file.

thanga
  • 163
  • 2
  • 13
-1

Before using sed command, you can use \ as escape character then it will print " in your string. As an example, i put the similar string to first line of a text file with below command:

var = "\"name,id,3\""
sed -i '1i' "$var" /tmp/out.txt
Mustafa Kemal
  • 1,292
  • 19
  • 24
-1

This works for me:

echo name,id,3 | sed 's/name,id,3/"name,id,3"/'
"name,id,3"
Anish
  • 189
  • 2
  • 6