Bash - how to put each line within quotation
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
Comments
-
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 almost 11 yearsI tried this and discovered that, for some reason, on blank lines it only output one quote.
-
glenn jackman almost 11 yearsfor the
read
solution, you needIFS= read -r FOO
. Otherwise you'll lose whitespace at the beginning of lines and backslash-escapes my be lost -
glenn jackman almost 11 yearsCould do
sed 's/.*/"&"/'
-
choroba almost 11 years@glennjackman: Yes. If the line is empty, there is only one position when the substitution can be tried.
-
kojiro almost 11 yearsOr lose the
FOO
and use the implicit$REPLY
name. Just dowhile read; do printf '"%s"\n' "$REPLY"; done < inputfile
-
michael almost 11 yearssed 's/^/"/; s/$/"/' file, assuming no line is already quoted.
-
blue almost 11 yearsall more reasons to go with
awk
as I see it :D -
kojiro almost 11 years@glennjackman sure, if you actually want to retain backslash escapes.
-
grepit almost 11 yearsit would be nice to at least provide some explanation as why you feel this is incorrect.
-
chepner almost 11 yearsOther 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 almost 11 years@ chepner I have modified my comment and thanks for giving me a feedback so I can contribute more effectively.
-
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 almost 7 yearsConsider 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 about 6 yearsYou're right Mr. macetw, my mistake not put clear that's just an command example.
-
Bob over 5 yearsUsing mingw xargs 4.4.2 on windows 10 from command prompt found >xargs -I{lin} echo "'"lin"'" worked for single quotes.
-
ApproachingDarknessFish over 3 yearsI 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
\\\"
.