Bash - how to put each line within quotation

35,118

Solution 1

Using awk

awk '{ print "\""$0"\""}' inputfile

Using pure bash

while read FOO; do
   echo -e "\"$FOO\""
done < inputfile

where inputfile would be a file containing the lines without quotes.

If your file has empty lines, awk is definitely the way to go:

awk 'NF { print "\""$0"\""}' inputfile

NF tells awk to only execute the print command when the Number of Fields is more than zero (line is not empty).

Solution 2

I use the following command:

xargs -I{lin} echo \"{lin}\" < your_filename

The xargs take standard input (redirected from your file) and pass one line a time to {lin} placeholder, and then execute the command at next, in this case a echo with escaped double quotes.

You can use the -i option of xargs to omit the name of the placeholder, like this:

xargs -i echo \"{}\" < your_filename

In both cases, your IFS must be at default value or with '\n' at least.

Solution 3

This sed should work for ignoring empty lines as well:

sed -i.bak 's/^..*$/"&"/' inFile

or

sed 's/^.\{1,\}$/"&"/' inFile

Solution 4

Use sed:

sed -e 's/^\|$/"/g' file

More effort needed if the file contains empty lines.

Solution 5

paste -d\" /dev/null your-file /dev/null

(not the nicest looking, but probably the fastest)

Now, if the input may contain quotes, you may need to escape them with backslashes (and then escape backslashes as well) like:

sed 's/["\]/\\&/g; s/.*/"&"/' your-file
Share:
35,118
Bing.Physics
Author by

Bing.Physics

A novice programmer

Updated on July 09, 2022

Comments

  • Bing.Physics
    Bing.Physics almost 2 years

    I want to put each line within quotation marks, such as:

    abcdefg
    hijklmn
    opqrst
    

    convert to:

    "abcdefg"
    "hijklmn"
    "opqrst"
    

    How to do this in Bash shell script?

  • glenn jackman
    glenn jackman almost 11 years
    I tried this and discovered that, for some reason, on blank lines it only output one quote.
  • glenn jackman
    glenn jackman almost 11 years
    for the read solution, you need IFS= read -r FOO. Otherwise you'll lose whitespace at the beginning of lines and backslash-escapes my be lost
  • glenn jackman
    glenn jackman almost 11 years
    Could do sed 's/.*/"&"/'
  • choroba
    choroba almost 11 years
    @glennjackman: Yes. If the line is empty, there is only one position when the substitution can be tried.
  • kojiro
    kojiro almost 11 years
    Or lose the FOO and use the implicit $REPLY name. Just do while read; do printf '"%s"\n' "$REPLY"; done < inputfile
  • michael
    michael almost 11 years
    sed 's/^/"/; s/$/"/' file, assuming no line is already quoted.
  • blue
    blue almost 11 years
    all more reasons to go with awk as I see it :D
  • kojiro
    kojiro almost 11 years
    @glennjackman sure, if you actually want to retain backslash escapes.
  • grepit
    grepit almost 11 years
    it would be nice to at least provide some explanation as why you feel this is incorrect.
  • chepner
    chepner almost 11 years
    Other than showing how to output a literal double quotation mark, it does not address the topic of adding quotation marks to preexisting text at all.
  • grepit
    grepit almost 11 years
    @ chepner I have modified my comment and thanks for giving me a feedback so I can contribute more effectively.
  • Stephane Chazelas
    Stephane Chazelas almost 11 years
    s/line is not empty/line is not blank/. Note that the -e is not needed and will actually alter the content if it contains backslashes.
  • macetw
    macetw almost 7 years
    Consider also, that one might not really need to echo the thing, but one might have needed quote-marks to do other operations in the script. For me, I can run a command: script-that-lists-files-per-line.py | xargs -I{lin} sudo chmod a+r {lin}
  • 0zkr PM
    0zkr PM about 6 years
    You're right Mr. macetw, my mistake not put clear that's just an command example.
  • Bob
    Bob over 5 years
    Using mingw xargs 4.4.2 on windows 10 from command prompt found >xargs -I{lin} echo "'"lin"'" worked for single quotes.
  • ApproachingDarknessFish
    ApproachingDarknessFish over 3 years
    I appreciate this solution because it only required me to escape a single quotation mark when embedding the command where I needed to escape every one as \\\".