如何使用 bash 脚本和 sed 用换行符替换字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23522132/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:49:55  来源:igfitidea点击:

How do I replace a string with a newline using a bash script and sed?

bashshellunixsed

提问by Kevin Custer

I have the following input:

我有以下输入:

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

In my bash script I would like to replace the @@ with a newline. I have tried various things with sed but I'm not having any luck:

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

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

Ultimately I need to parse this whole input into rows and values. Maybe I am going about it wrong. I was planning to replace the @@ with newlines and then loop through the input with setting IFS='|'to split up the values. If there is a better way please tell me, I am still a beginner with shell scripting.

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

回答by prudviraj

This will work

这将工作

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

replaces @@with new line

替换@@为新行

回答by anubhava

Using pure BASH string manipulation:

使用纯 BASH 字符串操作:

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

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

回答by itzhaki

I recommend using the trfunction

我建议使用tr函数

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

For example:

例如:

[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

回答by Kevin Custer

Finally got it working with:

终于让它与:

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

Adding the single quotes around \\n seemed to help for whatever reason

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

回答by Victor Henriquez

If you don't mind to use perl:

如果您不介意使用 perl:

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

回答by Tripp Kinetics

How about:

怎么样:

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

回答by Brad Parks

This wraps up using perl to do it, and gives some simple help.

这结束了使用 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

替换字符串.sh

#!/bin/bash

ME=$(basename ##代码##)
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 [ "" == "help" ]
then
  show_help
fi
if [ -z "" ]
then
  show_help
fi

STRING=""
TIMES=${3:-""}
WITH=${2:-"\n"}

if [ "$TIMES" == "all" ]
then
  TIMES="g"
else
  TIMES=""
fi

perl -pe "s/$STRING/$WITH/$TIMES"