为什么不“ sed”接听命令?

时间:2019-06-13 16:43:18

标签: bash sed

`使用sed并尝试拉出特定行时,它会失去“ sed -n(x)p test.txt”的p部分

我正在尝试查看一行,看看它是A还是B。

sed -n 3p test.txt

工作正常,但我正在尝试:

sed -n $(Count) test.txt

这不起作用

sed -n $($Count)p test.txt

不起作用

Count=$(cat -n test.txt | grep -o [0-9]* | tail -1)

until [ $Count = 0 ]; do
    if [[ $(sed -n $(Count)p test.txt) = Him ]] || [[ $(sed -n $(Count)p model.txt) = He ]]
        then
        echo "This is a Boy Word"
    elif [[ $(sed -n $(Count)p model.txt) = Her ]] || [[ $(sed -n $(Count)p model.txt) = She ]]
        then
        echo "This is an Girl Word"
    fi
    let Count=Count-1
    sleep 1
done

我期望: 这是男孩的话

这是一个男孩单词

这是一个女生单词

这是一个女孩字... 直到一切都完成了,

However I'm getting (with sed -n $($Count)p test.txt)
Line 17: 3: command not found
Line 20: 3: command not found
Line 17: 2: command not found
Line 17: 2: command not found


Or (with sed -n $(Count)p test.txt
Line 17: Count: command not found
Line 20: Count: Command not found
Line 17: Count: Command not Found
Line 20: Count: command not found

1 个答案:

答案 0 :(得分:1)

您需要使用完整的格式${Count}来将变量名与相邻字符分开。

sed -n ${Count}p test.txt

或者,只需引用参数扩展:

sed -n "$Count"p test.txt