bash shell的脚本 - 把报价单​​中每一行 [英] Bash Shell Script--Put each line within quotation

查看:141
本文介绍了bash shell的脚本 - 把报价单​​中每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:我要加引号,如在每个行:

Question: 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

使用的纯庆典

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 shell的脚本 - 把报价单​​中每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆