How to add double quotes to a line with SED or AWK?

awk, regex, sed, shell, text-processing

Solution

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.

Problem

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.

Original source