【问题标题】:search a pattern in file and insert/append a string in specific position在文件中搜索模式并在特定位置插入/附加字符串
【发布时间】:2022-01-02 08:46:18
【问题描述】:

我对 sed 脚本完全陌生。我一直在研究如何将文本添加到文件并设法将我想要添加到文件中正确行的文本但找不到将其添加到正确位置的方法! 所以我在文本文件中的行看起来像这样

/^From: \s*(.*@)?(((test\.new\.com)))/ REJECT You are not me

我想从用户那里获取输入并将输入添加到上面的行,结果应该如下所示,用户输入是“test2.newer.com”,我想添加这个字符串|(test2\.newer\.com)

/^From: \s*(.*@)?(((test\.new\.com)|(test2.\newer\.com)))/ REJECT You are not me

我尝试了这个,但不起作用

read -p "Enter new domain: " newdomain
file="./testfile"
sed -i "/You are not me/ s/^\(.*\)\())\)/\1, |($newdomain)\2/" $file

如何将它添加到正确的位置?

【问题讨论】:

  • 删除| 标记周围的空格。 (您在示例之间也将 .com 更改为 .ir,我不知道这是否是故意的,只是想提一下)。
  • 但是对于更高级别的解决方案:考虑到/^From: ,我可以有根据地猜测您正在解析电子邮件。有很多现有的工具可以让您解析电子邮件,并以更有用的格式(例如可能是 JSON)给出结构化响应。使用正则表达式很可能是一个糟糕的选择(更难构建,更难维护,在边缘情况下不太可靠,......)
  • 你应该添加更多和某些细节。您没有写出要更改的文本。第一个代码行在命令中包含 test.new.com,另一方面在第二个代码行中包含 test.new.ir。
  • 实际上我找到了这个解决方案,但它不起作用 sed -i "/You are not me/ s/^(.*)()))/\1, |($newdomain)\2/ " $文件
  • how to add text to a file and managed to get the text I want adding to the correct line in the file 你能不能把它分成多个句子并分步解释你想要什么。您要添加什么文字?在哪里?到什么线?哪一个是正确的?正确的位置在哪里? i want to add new string "test2.newer.com"你似乎想添加|(test2.\newer\.com),而不是test2.newer.com

标签: linux bash shell


【解决方案1】:

使用Escape a string for a sed replace pattern,您可以替换任何您想要的行内容。

lineinfile='/^From: \s*(.*@)?(((test\.new\.com)))/ REJECT You are not me'
newcontent='/^From: \s*(.*@)?(((test\.new\.com)|(test2.\newer\.com)))/ REJECT You are not me'

ESCAPED_KEYWORD=$(printf '%s\n' "$lineinfile" | sed -e 's/[]\/$*.^[]/\\&/g');
ESCAPED_REPLACE=$(printf '%s\n' "$newlinecontent" | sed -e 's/[\/&]/\\&/g')
sed "s/$ESCAPED_KEYWORD/$ESCAPED_REPLACE/g"

【讨论】:

    【解决方案2】:

    使用sed

    $ newdomain="(test2.\\\newer\.com)"
    $ sed "s|\([^)]*.[^)]*)\)\(.*\)|\1\|$newdomain\2|" input_file
    /^From: \s*(.*@)?(((test\.new\.com)|(test2.\newer.com)))/ REJECT You are not me
    
    

    【讨论】:

      【解决方案3】:

      newdomain='test2\.newer\.com' 你可以使用awk 命令

      awk -v repl="${newdomain//\\/\\\\}" '
        BEGIN {OFS=FS=")))"}
        /You are not me/ {$1=$1 ")|(" repl}
        1
      ' "$file" > "$file".new && mv "$file".new "$file"
      

      解释:
      ${newdomain//\\/\\\\}"awk 想把转义序列\. 当作.,所以添加反斜杠。
      BEGIN {OFS=FS=")))"} 在解析行之前设置“)))”作为字段分隔符(输入和输出)。
      /You are not me/ 使用此子字符串更改行。
      $1=$1 ")|(" repl')|(repl 中的字符串附加到$1
      1 打印该行。
      "$file" > "$file".new && mv "$file".new "$file"awk 成功时,不要原地编辑而是重定向到一个新文件并将该文件移动到原始文件。

      【讨论】:

        【解决方案4】:

        谢谢大家,我发现我的问题和答案是这样的:

        read -p "Enter domain: " newdomain
        sed -i "/You are not me/ s/\()))\)/)|$newdomain)))/' $file
        

        但是输出是这样的:

        /^From: \s*(.*@)?(((test\.new\.com)|(test2.newer.com)))/ REJECT You are not me
        

        但我想要这个输出:

        /^From: \s*(.*@)?(((test\.new\.com)|(test2.\newer\.com)))/ REJECT You are not me
        

        【讨论】:

          猜你喜欢
          • 2020-01-20
          • 1970-01-01
          • 2012-08-12
          • 2016-05-02
          • 2012-12-01
          • 2011-08-08
          • 2021-11-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多