【问题标题】:How to add single quote after specific word using sed?如何使用sed在特定单词后添加单引号?
【发布时间】:2020-03-08 11:32:40
【问题描述】:

我正在尝试编写一个脚本以在“GOOD”单词之后添加单引号。 例如,我有 file1 :

//WER GOOD=ONE //WER1 GOOD=TWO2 //PR1 GOOD=THR45 ...

期望的改变是添加单引号:

//WER GOOD='ONE' //WER1 GOOD='TWO2' //PR1 GOOD='THR45' ...

这是我要运行的脚本:

#!/bin/bash

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

do

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

done 

提前感谢您的帮助!

【问题讨论】:

    标签: bash shell awk sed scripting


    【解决方案1】:

    请您尝试关注一下。

    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.
    

    如果您想将输出保存到 Input_file 本身,请在上述代码中使用-i 选项。



    awk 的第二个解决方案:

    awk 'match($0,/=.*/){$0=substr($0,1,RSTART) "\047" substr($0,RSTART+1,RLENGTH) "\047"} 1' Input_file
    

    说明:为上述代码添加说明。

    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.
    


    第三种解决方案:如果您的 Input_file 中只有 2 个字段,请尝试更简单的 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.
    

    【讨论】:

    • RavinderSingh13 - 感谢您的回复!我已经使用了您提供的第一个 sed 命令,它正在添加引号。但是,添加引号后,它会在下面创建一个更多的空间。因此,我尝试将 :space: 添加到 sed 命令中,如下所示:sed "s/\(.*=\)\(.*\)[[:space:]]/\1'\2'/" Input_file。它仍在添加下面的空间。你知道我该如何解决吗?
    • @Sunny,当我使用您提供的示例测试此代码时,它对我来说效果很好,不确定您的 Input_file 是否已经有空格?或者你有控制M个字符?能否请你运行命令cat -v Input_file 看看你是否有控制 M 字符让我知道它?
    • RavinderSingh13 当我执行 cat -v Input_file 时,它没有显示任何额外的空格。我想在此文件中更改的任何内容都在下面添加空格。例如,之前在 file1 中,我将 %%WER 更改为 //WER 并添加 [[:space:]] 到 sed 命令,因为没有,它在下面添加空格。
    • @Sunny,我真的不确定你之前的命令,我的代码只关注你展示的示例,这对我来说很好。如果您将任何其他命令的输出发送到此新命令,请确保该命令本身不应包含空格等。我还添加了 OR sed 解决方案,它删除了带空格的行(NULL 行)让我知道现在情况如何?
    • RavinderSingh13 谢谢大家的解释!绝对有帮助!我只需要使用 sed 命令。该命令正在添加引号,但是结束单引号在一些空格之后完成。在您提供的 sed 命令中,是否可以对 GOOD= 之后的单词进行 grep 并替换?那么,除了给出(.*\),是否可以给出紧跟在 GOOD= 之后的确切字符串?因为当我做 * 时,它也会占用所有空间。
    猜你喜欢
    • 1970-01-01
    • 2018-12-03
    • 2019-12-29
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2011-12-25
    相关资源
    最近更新 更多