whi*_*edb 14 command-line bash
是否有任何简短的方法可以将管道输出保存到它处理的同一个文件中。例如,这就是我实际在做的事情
$ cat filename | sort | uniq > result
$ rm -f filename
$ mv result filename
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种方法可以在一行中完成(不使用 && 附加这些命令)
这不是方法,而是获得一个想法
$ cat filename | sort | uniq > filename
Run Code Online (Sandbox Code Playgroud)
cuo*_*glm 18
您可以sponge从moreutils包中使用:
LC_ALL=C sort -u filename | sponge filename
Run Code Online (Sandbox Code Playgroud)
您也不需要管道到uniq,因为 when在排序时sort可以-u选择唯一行。
请注意,在具有 UTF-8 语言环境的 GNU 系统上,sort -u或者sort | uniq没有为您提供唯一的行,而是第一个在当前语言环境中排序相同的行序列。
$ printf '%b\n' '\U2460' '\U2461' | LC_ALL=en_US.utf8 sort | LC_ALL=en_US.utf8 uniq
?
Run Code Online (Sandbox Code Playgroud)
只给了你?。将语言环境更改为 C 强制基于字节值的排序顺序:
$ export LC_ALL=C
$ printf '%b\n' '\U2460' '\U2461' | LC_ALL=C sort | LC_ALL=C uniq
?
?
Run Code Online (Sandbox Code Playgroud)
αғs*_*нιη 13
您不需要任何额外的命令,例如catand uniq,也不需要使用rmcommand 和mvcommand 来删除和重命名文件名。只需使用简单的命令。
sort -u filename -o filename
Run Code Online (Sandbox Code Playgroud)
-u, --unique
with -c, check for strict ordering; without -c, output only the
first of an equal run
-o, --output=FILE
write result to FILE instead of standard output
Run Code Online (Sandbox Code Playgroud)
sort命令对您的文件名进行排序,并使用-u选项从中删除重复的行。然后 with-o选项使用就地方法将输出写入同一个文件。
| 归档时间: |
|
| 查看次数: |
13872 次 |
| 最近记录: |