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

0

Issue

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.

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.

Answered By – Dennis Williamson

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More