Linux Bash - 如何将每一行放在引号内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16991428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash - how to put each line within quotation
提问by Bing.Physics
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?
如何在 Bash shell 脚本中执行此操作?
回答by choroba
Use sed:
使用 sed:
sed -e 's/^\|$/"/g' file
More effort needed if the file contains empty lines.
如果文件包含空行,则需要更多的努力。
回答by blue
Using awk
使用awk
awk '{ print "\""while read FOO; do
echo -e "\"$FOO\""
done < inputfile
"\""}' inputfile
Using pure bash
使用纯 bash
awk 'NF { print "\""#!/bin/bash
chr="\""
file="file.txt"
cp $file $file."_backup"
while read -r line
do
echo "${chr}$line${chr}"
done <$file > newfile
mv newfile $file
"\""}' inputfile
where inputfile
would be a file containing the lines without quotes.
whereinputfile
将是一个包含没有引号的行的文件。
If your file has empty lines, awkis definitely the way to go:
如果您的文件有空行,则awk绝对是最佳选择:
sed -i.bak 's/^..*$/"&"/' inFile
NF
tells awk
to only execute the print command when the Number of Fields is more than zero (line is not empty).
NF
告诉awk
仅当字段数大于零(行不为空)时才执行打印命令。
回答by grepit
I think the sed and awk are the best solution but if you want to use just shell here is small script for you.
我认为 sed 和 awk 是最好的解决方案,但如果你只想使用 shell,这里是一个小脚本。
sed 's/^.\{1,\}$/"&"/' inFile
回答by anubhava
This sed should work for ignoring empty lines as well:
这个 sed 也适用于忽略空行:
paste -d\" /dev/null your-file /dev/null
or
或者
sed 's/["\]/\&/g; s/.*/"&"/' your-file
回答by Stephane Chazelas
xargs -I{lin} echo \"{lin}\" < your_filename
(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:
现在,如果输入可能包含引号,您可能需要用反斜杠转义它们(然后也转义反斜杠),例如:
xargs -i echo \"{}\" < your_filename
回答by 0zkr PM
I use the following command:
我使用以下命令:
$ bla=foo
$ sed -e "/${bla}/s#^#<P>#" -e "/${bla}/s#$#</P>#" inputfile
<P>foo</P>
bar
$
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.
该xargs
取标准输入(从文件重定向),并通过一个线时间{lin}
占位符,然后在接下来的执行命令,在这种情况下,echo
有逃脱双引号。
You can use the -i
option of xargs to omit the name of the placeholder, like this:
您可以使用-i
xargs 选项来省略占位符的名称,如下所示:
In both cases, your IFS must be at default value or with '\n'
at least.
在这两种情况下,您的 IFS 必须处于默认值或'\n'
至少为。
回答by DOK
I used sed with two expressions to replace start and end of line, since in my particular use case I wanted to place HTML tags around only lines that contained particular words.
我使用带有两个表达式的 sed 来替换行首和行尾,因为在我的特定用例中,我只想在包含特定单词的行周围放置 HTML 标签。
So I searched for the lines containing words contained in the bla
variable within the text file inputfile
and replaced the beginnign with <P>
and the end with </P>
(well actually I did some longer HTML tagging in the real thing, but this will serve fine as example)
所以我搜索了包含在bla
文本文件中的变量中包含的单词的行,inputfile
并替换了 beginnign<P>
和 end </P>
(实际上我在真实的东西中做了一些更长的 HTML 标记,但这可以作为例子)
Similar to:
相似:
##代码##