Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - how to put each line within quotation

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?

like image 924
Bing.Physics Avatar asked Jun 07 '13 19:06

Bing.Physics


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.

How do you string multiple quotes together?

Optional Rule # 4. Even if a paragraph lists quotes from more than one source, you can still summarize them into one reference placed after the last quote or at the end of the paragraph. In this case, separate the different authors listed in your reference by a semicolon, in both MLA and Chicago.


2 Answers

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

like image 143
blue Avatar answered Oct 04 '22 07:10

blue


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.

like image 41
0zkr PM Avatar answered Oct 04 '22 08:10

0zkr PM