如何从bash中的变量中删除文字字符串"\n"(不是换行符)?

Voi*_*oid 5 bash perl awk sed tr

我正在从数据库中提取一些数据,而我正在返回的其中一个字符串在一行中并包含多个字符串实例\n.这些不是换行符; 它们实际上是字符串\n,即反斜杠+ en或hex 5C 6E.

我已经尝试使用sed和tr来删除它们但是它们似乎没有识别字符串并且根本不影响变量.这是在谷歌上搜索的一个难题,因为我得到的所有结果都是关于如何从字符串中删除换行符,这不是我需要的.

如何在bash中从变量中删除这些字符串?

示例数据:

\n\nCreate a URL where the client can point their web browser to. This URL should test the following IP addresses and ports for connectivity.

示例失败的命令:

echo "$someString" | tr '\\n' ''
Run Code Online (Sandbox Code Playgroud)

操作系统:Solaris 10

可能重复 - 除了这是在python中

Sto*_*ica 5

我怀疑你在使用时没有\在更换中正确逃脱sed.另请注意,这tr不适合此任务.最后,如果你想更换\n一个变量,那么模式替换(形式参数扩展)是最好的选择.

要替换\n变量,可以使用Bash模式替换:

$ text='hello\n\nthere\nagain'
$ echo ${text//\\n/}
hellothereagain
Run Code Online (Sandbox Code Playgroud)

要替换\n标准输入,您可以使用sed:

$ echo 'hello\n\nthere\nagain' | sed -e 's/\\n//g'
hellothereagain
Run Code Online (Sandbox Code Playgroud)

请注意两个示例\中的模式中的转义\\.