本文介绍了Bash-如何将每一行放在引号内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将每行放在引号内,例如:

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

abcdefg
hijklmn
opqrst

转换为:

"abcdefg"
"hijklmn"
"opqrst"

如何在Bash shell脚本中执行此操作?

How to do this in Bash shell script?

推荐答案

使用 awk

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

使用纯bash

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

其中inputfile是一个包含不带引号的行的文件.

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

如果文件中有空行,则 awk 绝对是可行的方法:

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

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

NF告诉awk仅在字段数"大于零(行不为空)时才执行打印命令.

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

这篇关于Bash-如何将每一行放在引号内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:10