我想反转文本文件(或stdin)中的行顺序,保留每行的内容。

因此,从以下内容开始:

foo
bar
baz

我希望最后

baz
bar
foo

是否有用于此的标准UNIX命令行实用程序?


当前回答

$ (tac 2> /dev/null || tail -r)

试试tac,它在Linux上运行,如果不起作用,可以使用tail-r,它在BSD和OSX上运行。

其他回答

编辑下面生成从1到10的随机排序的数字列表:

seq 1 10 | sort -R | tee /tmp/lst |cat <(cat /tmp/lst) <(echo '-------') **...**

其中点被替换为实际命令,该命令反转列表

tac

seq 1 10 | sort -R | tee /tmp/lst |cat <(cat /tmp/lst) <(echo '-------') \
<(tac)

python:在sys.stdin上使用[::-1]

seq 1 10 | sort -R | tee /tmp/lst |cat <(cat /tmp/lst) <(echo '-------') \
<(python -c "import sys; print(''.join(([line for line in sys.stdin])[::-1]))")

碰巧我想高效地获取一个非常大的文本文件的最后n行。

我尝试的第一件事是tail-n 10000000 file.txt>ans.txt,但我发现它非常慢,因为tail必须查找到位置,然后返回打印结果。

当我意识到这一点时,我切换到另一个解决方案:tac file.txt | head-n 10000000>ans.txt。这一次,搜索位置只需要从末端移动到所需位置,它节省了50%的时间!

带回家消息:

如果尾部没有-r选项,请使用tac-file.txt | head-n n。

你可以用vim-stdin和stdout来实现。您也可以使用ex来兼容POSIX。vim只是ex的视觉模式。事实上,您可以将ex与vim-e或vim-e(改进的ex模式)一起使用。vim很有用,因为与sed等工具不同,它缓冲文件以供编辑,而sed用于流。您可能可以使用awk,但必须手动缓冲变量中的所有内容。

其目的是:

从stdin读取对于每一行,将其移至第1行(反向)。命令为g/^/m0。这意味着对于每一行g;匹配行的开头,它匹配任何^;将其移到地址0之后,即第1m0行。打印所有内容。命令为%p。这意味着所有行的范围为%;打印行p。强制退出而不保存文件。命令是q!。这意味着退出q;用力!。

# Generate a newline delimited sequence of 1 to 10
$ seq 10
1
2
3
4
5
6
7
8
9
10

# Use - to read from stdin.
# vim has a delay and annoying 'Vim: Reading from stdin...' output
# if you use - to read from stdin. Use --not-a-term to hide output.
# --not-a-term requires vim 8.0.1308 (Nov 2017)
# Use -E for improved ex mode. -e would work here too since I'm not
# using any improved ex mode features.
# each of the commands I explained above are specified with a + sign
# and are run sequentially.
$ seq 10 | vim - --not-a-term -Es +'g/^/m0' +'%p' +'q!'
10
9
8
7
6
5
4
3
2
1
# non improved ex mode works here too, -e.
$ seq 10 | vim - --not-a-term -es +'g/^/m0' +'%p' +'q!'

# If you don't have --not-a-term, use /dev/stdin
seq 10 | vim -E +'g/^/m0' +'%p' +'q!' /dev/stdin

# POSIX compliant (maybe)
# POSIX compliant ex doesn't allow using + sign to specify commands.
# It also might not allow running multiple commands sequentially.
# The docs say "Implementations may support more than a single -c"
# If yours does support multiple -c
$ seq 10 | ex -c "execute -c 'g/^/m0' -c '%p' -c 'q!' /dev/stdin

# If not, you can chain them with the bar, |. This is same as shell
# piping. It's more like shell semi-colon, ;.
# The g command consumes the |, so you can use execute to prevent that.
# Not sure if execute and | is POSIX compliant.
seq 10 | ex -c "execute 'g/^/m0' | %p | q!" /dev/stdin

如何使其可重复使用

我使用一个我称为ved的脚本(vim编辑器,如sed)来使用vim编辑stdin。将其添加到路径中名为ved的文件中:

#!/usr/bin/env sh

vim - --not-a-term -Es "$@" +'%p | q!'

我使用的是一个+命令,而不是+'%p'+'q!',因为vim将您限制为10个命令。因此,合并它们可以让“$@”有9个以上的命令,而不是8个。

然后您可以执行以下操作:

seq 10 | ved +'g/^/m0'

如果您没有vim 8,请将其改为:

#!/usr/bin/env sh

vim -E "$@" +'%p | q!' /dev/stdin

还值得一提的是:tac(猫的反面)。部分coreutils。

将一个文件翻成另一个文件

tac a.txt > b.txt

我真的很喜欢“尾巴-r”的答案,但我最喜欢的呆呆的答案是。。。。

gawk '{ L[n++] = $0 } 
  END { while(n--) 
        print L[n] }' file