Bash - how to put each line within quotation

后端 未结 7 1293
既然无缘
既然无缘 2020-12-30 21:14

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

abcdefg
hijklmn
opqrst

convert to:

\"abcdefg\"
\"hijklmn\"
\"opqrs         


        
7条回答
  •  既然无缘
    2020-12-30 21:36

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

提交回复
热议问题