带有 head -n 的 xargs

xargs with head -n

我正在尝试编写一个命令,它从 CSV 中获取随机行并输出该行负 5。比如说,它输出第 50 行,然后它应该打印从 46 到 50 两者。我可以分两步实现这一点,但是当尝试使用“xargs”将行号传递给以下命令 (head -n) 时,我收到一条错误消息:"head: invalid number of lines: ‘{}’"。 我的命令是:

cat test.csv |  awk '{ print NR }' | shuf -n 1 | xargs -l head -n "{}" test.csv | tail -n 5

其中 "xargs -l head -n "{}" test.csv" 应该从 "shuf" 函数中收集数字,然后将其拖到 5 以获取最后 5 行。

我做错了什么?

谢谢

引用 xargs 手册页:

-I replace-str

Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1.

如您所见,-I 标志是指定命令的替换字符串所必需的。

如果没有指定的字符串,xargs 将简单地将每一行的值附加到命令的末尾。所以以下应该有效:

cat test.csv |  awk '{ print NR }' | shuf -n 1 | xargs -I {} head -n "{}" test.csv | tail -n 5