为什么这个sed命令在bash脚本中什么也不做,但在外面工作?

时间:2022-02-22 22:11:39
 # join pairs of lines side-by-side (like "paste")
 sed '$!N;s/\n/ /'

The above script comes from the great list of sed one-liners found on sourceforge.

上面的脚本来自sourceforge上发现的sed one-liners的优秀列表。

I wish to use it in an a bash script but it has no effect if used inside the script. If I pipe the output of the script through it, it joins join pairs of lines side-by-side as described.

我希望在一个bash脚本中使用它,但如果在脚本中使用它则无效。如果我通过它传递脚本的输出,它将如所描述的那样并排连接线对。

Some character must need escaping but I just can't "see" which character needs to be escaped to make it work inside a bash script.

某些角色必须要逃避,但我无法“看到”需要转义哪个角色才能使其在bash脚本中运行。

Yoroshiku Onegaishimasu!

Later..

#!/bin/bash
# numbers.sh

for X in 1 2 3 4 5 6 7 8 9 0
do
        echo $X
done

When used this script:

使用此脚本时:

#!/bin/bash

./numbers.sh | sed '$!N;s/\n/ /'

works fine..

1 2
3 4
5 6
7 8
9 0

Please let me regroup my thoughts on this..

请让我重新组合我的想法..

Later...

I found the logical error in the script which broke it.

我发现脚本中的逻辑错误打破了它。

2 个解决方案

#1


It's not obvious to me what the problem is without seeing your script. A quick test here and it worked just fine inside of a simple script:

如果没有看到你的脚本,问题是什么并不明显。这里有一个快速测试,它在一个简单的脚本中运行得很好:

#!/bin/bash
cat /etc/crontab | sed '$!N;s/\n/ /'

If you're trying to embed the command inside a string or variable, the \n will be an escape candidate.

如果您尝试将命令嵌入字符串或变量中,\ n将成为转义候选者。

For what it's worth, there's rarely a 'strong' case for making bash-specific scripts over straight up /bin/sh posix-compliant shell scripts unless you really need the advanced containers (which is rare). You'll end up with a script that is considerably more portable to dozens of other posix/korn/bourne-compatible shells (including bash).

对于它的价值,除了你真的需要高级容器(这是罕见的)之外,很少有“强大”的情况下使用直接/ bin / sh posix兼容的shell脚本制作特定于bash的脚本。你最终会得到一个脚本,它比其他几十个posix / korn / bourne兼容的shell(包括bash)更加便携。

Cheers! Sean

#2


Are you missing the "$@" to indicate the file name arguments - so it was only reading from standard input?

你是否错过了“$ @”来表示文件名参数 - 所以它只是从标准输入读取?

What was the misbehaviour? Was the file simply copied to standard output?

什么是不端行为?该文件是否只是复制到标准输出?

Works for me - under Cygwin. 'al' is a program that lists its arguments one per line.

适合我 - 在Cygwin下。 'al'是一个程序,每行列出一个参数。

$ al a b c d e f  | sed '$!N;s/\n/ /'
a b
c d
e f
$ cat xxx
sed '$!N;s/\n/ /'
$ al a b c d e f g | bash xxx
a b
c d
e f
g
$

#1


It's not obvious to me what the problem is without seeing your script. A quick test here and it worked just fine inside of a simple script:

如果没有看到你的脚本,问题是什么并不明显。这里有一个快速测试,它在一个简单的脚本中运行得很好:

#!/bin/bash
cat /etc/crontab | sed '$!N;s/\n/ /'

If you're trying to embed the command inside a string or variable, the \n will be an escape candidate.

如果您尝试将命令嵌入字符串或变量中,\ n将成为转义候选者。

For what it's worth, there's rarely a 'strong' case for making bash-specific scripts over straight up /bin/sh posix-compliant shell scripts unless you really need the advanced containers (which is rare). You'll end up with a script that is considerably more portable to dozens of other posix/korn/bourne-compatible shells (including bash).

对于它的价值,除了你真的需要高级容器(这是罕见的)之外,很少有“强大”的情况下使用直接/ bin / sh posix兼容的shell脚本制作特定于bash的脚本。你最终会得到一个脚本,它比其他几十个posix / korn / bourne兼容的shell(包括bash)更加便携。

Cheers! Sean

#2


Are you missing the "$@" to indicate the file name arguments - so it was only reading from standard input?

你是否错过了“$ @”来表示文件名参数 - 所以它只是从标准输入读取?

What was the misbehaviour? Was the file simply copied to standard output?

什么是不端行为?该文件是否只是复制到标准输出?

Works for me - under Cygwin. 'al' is a program that lists its arguments one per line.

适合我 - 在Cygwin下。 'al'是一个程序,每行列出一个参数。

$ al a b c d e f  | sed '$!N;s/\n/ /'
a b
c d
e f
$ cat xxx
sed '$!N;s/\n/ /'
$ al a b c d e f g | bash xxx
a b
c d
e f
g
$