Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何使用sed在特定单词后添加单引号?_Bash_Shell_Awk_Sed_Scripting - Fatal编程技术网

Bash 如何使用sed在特定单词后添加单引号?

Bash 如何使用sed在特定单词后添加单引号?,bash,shell,awk,sed,scripting,Bash,Shell,Awk,Sed,Scripting,我正试图写一个脚本,在一个“好”字后加上一个引号。 例如,我有文件1: //WER GOOD=1 //WER1 GOOD=TWO2 //PR1良好=THR45 … 所需的更改是添加单引号: //wergood='ONE' //我们都很好 //PR1良好class='THR45' … 这是我正在尝试运行的脚本: #!/bin/bash for item in `grep "GOOD" file1 | cut -f2 -d '='` do sed -i 's/$item/`\$item/`\/

我正试图写一个脚本,在一个“好”字后加上一个引号。 例如,我有文件1:

//WER GOOD=1
//WER1 GOOD=TWO2
//PR1良好=THR45
…

所需的更改是添加单引号:

//wergood='ONE'
//我们都很好
//PR1良好class='THR45'
…

这是我正在尝试运行的脚本:

#!/bin/bash

for item in `grep "GOOD" file1 | cut -f2 -d '='`

do

sed -i 's/$item/`\$item/`\/g' file1

done 

提前感谢您的帮助

你能试试下面的吗

sed "s/\(.*=\)\(.*\)/\1'\2'/"  Input_file
或根据OP的意见删除空行使用:

sed "s/\(.*=\)\(.*\)/\1'\2'/;/^$/d"  Input_file
解释:以下仅用于解释目的

sed "              ##Starting sed command from here.
s/                 ##Using s to start substitution process from here.
\(.*=\)\(.*\)      ##Using sed buffer capability to store matched regex into memory, saving everything till = in 1st buffer and rest of line in 2nd memory buffer.
/\1'\2'            ##Now substituting 1st and 2nd memory buffers with \1'\2' as per OP need adding single quotes before = here.
/"  Input_file     ##Closing block for substitution, mentioning Input_file name here.
请在上述代码中使用
-i
选项,以防将输出保存到输入文件本身



使用awk的第二个解决方案:

解释:为上述代码添加解释

awk '
match($0,/=.*/){                                                     ##Using match function to mmatch everything from = to till end of line.
  $0=substr($0,1,RSTART) "\047" substr($0,RSTART+1,RLENGTH) "\047"   ##Creating value of $0 with sub-strings till value of RSTART and adding ' then sub-strings till end of line adding ' then as per OP need.
}                                                                    ##Where RSTART and RLENGTH are variables which will be SET once a TRUE matched regex is found.
1                                                                    ##1 will print edited/non-edited line.
' Input_file                                                         ##Mentioning Input_file name here.


第三种解决方案:如果您的输入文件中只有两个字段,请在
awk
中尝试更简单的方法:

awk 'BEGIN{FS=OFS="="} {$2="\047" $2 "\047"} 1' Input_file
第三个代码的解释:仅用于解释目的,运行时请使用上述代码本身

awk '                    ##Starting awk program here.
BEGIN{FS=OFS="="}        ##Setting FS and OFS values as = for all line for Input_file here.
{$2="\047" $2 "\047"}    ##Setting $2 value with adding a ' $2 and then ' as per OP need.
1                        ##Mentioning 1 will print edited/non-edited lines here.
' Input_file             ##Mentioning Input_file name here.

你能试试下面的吗

sed "s/\(.*=\)\(.*\)/\1'\2'/"  Input_file
或根据OP的意见删除空行使用:

sed "s/\(.*=\)\(.*\)/\1'\2'/;/^$/d"  Input_file
解释:以下仅用于解释目的

sed "              ##Starting sed command from here.
s/                 ##Using s to start substitution process from here.
\(.*=\)\(.*\)      ##Using sed buffer capability to store matched regex into memory, saving everything till = in 1st buffer and rest of line in 2nd memory buffer.
/\1'\2'            ##Now substituting 1st and 2nd memory buffers with \1'\2' as per OP need adding single quotes before = here.
/"  Input_file     ##Closing block for substitution, mentioning Input_file name here.
请在上述代码中使用
-i
选项,以防将输出保存到输入文件本身



使用awk的第二个解决方案:

解释:为上述代码添加解释

awk '
match($0,/=.*/){                                                     ##Using match function to mmatch everything from = to till end of line.
  $0=substr($0,1,RSTART) "\047" substr($0,RSTART+1,RLENGTH) "\047"   ##Creating value of $0 with sub-strings till value of RSTART and adding ' then sub-strings till end of line adding ' then as per OP need.
}                                                                    ##Where RSTART and RLENGTH are variables which will be SET once a TRUE matched regex is found.
1                                                                    ##1 will print edited/non-edited line.
' Input_file                                                         ##Mentioning Input_file name here.


第三种解决方案:如果您的输入文件中只有两个字段,请在
awk
中尝试更简单的方法:

awk 'BEGIN{FS=OFS="="} {$2="\047" $2 "\047"} 1' Input_file
第三个代码的解释:仅用于解释目的,运行时请使用上述代码本身

awk '                    ##Starting awk program here.
BEGIN{FS=OFS="="}        ##Setting FS and OFS values as = for all line for Input_file here.
{$2="\047" $2 "\047"}    ##Setting $2 value with adding a ' $2 and then ' as per OP need.
1                        ##Mentioning 1 will print edited/non-edited lines here.
' Input_file             ##Mentioning Input_file name here.

胡言乱语13-感谢您的回复!我使用了您提供的第一个sed命令,它添加了quote fine。但是,在添加报价后,它会在下面多创建一个空间。因此,我尝试按如下方式将:space:添加到sed命令:
sed“s/\(.*=\)\(.*\)[[:space:]/\1'\2'/“Input\u文件
。它仍然在增加下面的空间。你知道我如何修复它吗?@Sunny,当我用你提供的示例测试这段代码时,它对我来说很好,不确定你的输入文件是否已经有空格了?或者你有控制M个字符?你能运行命令
cat-v输入文件
看看里面是否有控制字符,让我知道吗?RavinderSingh13当我执行
cat-v输入文件
时,它没有显示任何额外的空格。我想在这个文件中更改的任何内容,都是在下面添加空格。例如,在file1之前,我将
%%WER
更改为
//WER
,向sed命令添加了[[:space:],因为没有,它在下面添加了空格。@Sunny,我真的不确定您以前的命令,我的代码只关注于您显示的示例,这对我来说很好。如果要将任何其他命令的输出发送到此新命令,请确保该命令本身不应包含空格等。我还添加了或
sed
解决方案,该解决方案删除了带有空格的一行(空行),让我知道它现在是如何运行的?RavinderSingh13感谢您的所有解释!绝对有帮助!我只需要使用sed命令。该命令正在添加引号,但是在一些空格后结束单引号。在您提供的sed命令中,是否可以grep GOOD=后面的单词并替换?因此,不必给出
(.*\)
,是否可以给出GOOD=后面的确切字符串?因为当我这么做的时候,它也占用了所有的空间!我使用了您提供的第一个sed命令,它添加了quote fine。但是,在添加报价后,它会在下面多创建一个空间。因此,我尝试按如下方式将:space:添加到sed命令:
sed“s/\(.*=\)\(.*\)[[:space:]/\1'\2'/“Input\u文件
。它仍然在增加下面的空间。你知道我如何修复它吗?@Sunny,当我用你提供的示例测试这段代码时,它对我来说很好,不确定你的输入文件是否已经有空格了?或者你有控制M个字符?你能运行命令
cat-v输入文件
看看里面是否有控制字符,让我知道吗?RavinderSingh13当我执行
cat-v输入文件
时,它没有显示任何额外的空格。我想在这个文件中更改的任何内容,都是在下面添加空格。例如,在file1之前,我将
%%WER
更改为
//WER
,向sed命令添加了[[:space:],因为没有,它在下面添加了空格。@Sunny,我真的不确定您以前的命令,我的代码只关注于您显示的示例,这对我来说很好。如果要将任何其他命令的输出发送到此新命令,请确保该命令本身不应包含空格等。我还添加了或
sed
解决方案,该解决方案删除了带有空格的一行(空行),让我知道它现在是如何运行的?RavinderSingh13感谢您的所有解释!绝对有帮助!我只需要使用sed命令。该命令正在添加引号,但是在一些空格后结束单引号。在您提供的sed命令中,是否可以grep GOOD=后面的单词并替换?因此,不必给出
(.*\)
,是否可以给出GOOD=后面的确切字符串?因为当我这样做的时候,它也占用了所有的空间。