【问题标题】:How do I replace a string with a newline using a bash script and sed?如何使用 bash 脚本和 sed 将字符串替换为换行符?
【发布时间】:2020-07-10 04:09:53
【问题描述】:

我有以下输入:

Value1|Value2|Value3|Value4@@ Value5|Value6|Value7|Value8@@ Value9|etc...

在我的 bash 脚本中,我想用换行符替换 @@ 。我用 sed 尝试了各种方法,但没有运气:

line=$(echo ${x} | sed -e $'s/@@ /\\\n/g')

最终我需要将整个输入解析为行和值。也许我做错了。我打算用换行符替换@@ ,然后通过设置IFS='|' 循环输入以拆分值。如果有更好的方法请告诉我,我仍然是 shell 脚本的初学者。

【问题讨论】:

  • a gnu sed 的最新版本应该与 ...|sed 's/@@ /\n/g'.. 一起使用。祝你好运。
  • awk 与 RS="@@" 和 FS="|"

标签: regex bash shell unix sed


【解决方案1】:

这会起作用

sed 's/@@ /\n/g' filename

用新行替换@@

【讨论】:

  • @rpax 感谢编辑忘记格式化我的答案
  • 不客气。无论如何,这是一个很好的答案。我喜欢。 +1
  • 你不必转义第一个@,这在@@之后缺少一个空格。
【解决方案2】:

使用纯 BASH 字符串操作:

eol=$'\n'
line="${line//@@ /$eol}"

echo "$line"
Value1|Value2|Value3|Value4
Value5|Value6|Value7|Value8
Value9|etc...

【讨论】:

  • 遗憾的是这不起作用......它根本没有取代“@@”line="${x//@@ /$eol}"
  • @odinsride:确保您使用 BASH。查看工作演示:ideone.com/ePzOxq
【解决方案3】:

我推荐使用tr 函数

echo "$line" | tr '@@' '\n'

例如:

[itzhaki@local ~]$ X="Value1|Value2|Value3|Value4@@ Value5|Value6|Value7|Value8@@"
[itzhaki@local ~]$ X=`echo "$X" | tr '@@' '\n'`
[itzhaki@local ~]$ echo "$X"
Value1|Value2|Value3|Value4

 Value5|Value6|Value7|Value8

【讨论】:

  • 这在我的脚本中不起作用,它仍然在输出中包含“@@”:echo $x | tr '@@ ' '\n' >> $OUTPUT
  • 我知道它确实有效。我一直在使用“tr”。确保您使用的是 BASH 而不是 SH,并且您正在查看正确的输出。在这里,运行这个:pastebin.com/gxvKUwS9
  • 据我所知, tr 适用于单个字符,而不是字符串。如,tr 'Ab' 'aB'Abracadabra 变成aBracadaBra
  • 你只需要“挤”一下:tr -s '@' '\n'
  • 问题要求替换@@ 加上一个空白tr 做不到。
【解决方案4】:

如果你不介意使用 perl:

echo $line | perl -pe 's/@@/\n/g'
Value1|Value2|Value3|Value4
 Value5|Value6|Value7|Value8
 Value9|etc

【讨论】:

    【解决方案5】:

    终于搞定了:

    sed 's/@@ /'\\\n'/g'
    

    无论出于何种原因,在 \\n 周围添加单引号似乎都有帮助

    【讨论】:

      【解决方案6】:

      怎么样:

      for line in `echo $longline | sed 's/@@/\n/g'` ; do
          $operation1 $line
          $operation2 $line
          ...
          $operationN $line
          for field in `echo $each | sed 's/|/\n/g'` ; do
              $operationF1 $field
              $operationF2 $field
              ...
              $operationFN $field
          done
      done
      

      【讨论】:

      • 编辑答案以使解决方案与对这些行中的行和字段执行操作兼容。
      【解决方案7】:

      本文总结了使用 perl 来完成,并提供了一些简单的帮助。

      $ echo "hi\nthere"
      hi
      there
      
      $ echo "hi\nthere" | replace_string.sh e
      hi
      th
      re
      
      $ echo "hi\nthere" | replace_string.sh hi
      
      
      there
      
      $ echo "hi\nthere" | replace_string.sh hi bye
      bye
      there
      
      $ echo "hi\nthere" | replace_string.sh e super all
      hi
      thsuperrsuper
      

      replace_string.sh

      #!/bin/bash
      
      ME=$(basename $0)
      function show_help()
      {
        IT=$(cat <<EOF
      
        replaces a string with a new line, or any other string, 
        first occurrence by default, globally if "all" passed in
      
        usage: $ME SEARCH_FOR {REPLACE_WITH} {ALL}
      
        e.g. 
      
        $ME :       -> replaces first instance of ":" with a new line
        $ME : b     -> replaces first instance of ":" with "b"
        $ME a b all -> replaces ALL instances of "a" with "b"
        )
        echo "$IT"
        exit
      }
      
      if [ "$1" == "help" ]
      then
        show_help
      fi
      if [ -z "$1" ]
      then
        show_help
      fi
      
      STRING="$1"
      TIMES=${3:-""}
      WITH=${2:-"\n"}
      
      if [ "$TIMES" == "all" ]
      then
        TIMES="g"
      else
        TIMES=""
      fi
      
      perl -pe "s/$STRING/$WITH/$TIMES"
      

      【讨论】:

        猜你喜欢
        • 2022-07-21
        • 2020-07-23
        • 1970-01-01
        • 2016-04-30
        • 2017-09-06
        • 2019-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多