小编use*_*492的帖子

直接输出到管道和标准输出

有没有办法通过管道传输命令的输出并将其定向到stdout

例如,fortune打印一个幸运饼干stdout并将其通过管道传输到下一个命令:

$ fortune | tee >(?stdout?) | lolcat 
"...Unix, MS-DOS, and Windows NT (also known as the Good, the Bad, and
the Ugly)."
(By Matt Welsh)
Run Code Online (Sandbox Code Playgroud)

bash pipe tee stdout

27
推荐指数
2
解决办法
2万
查看次数

xargs | 使用输入作为命令

我会假设下面的例子工作得很好。

$ which -a python python3 pip | xargs -I {} {} --version
No '{}' such file or folder
$ which -a python python3 pip | xargs -I _ _ --version
No '_' such file or folder
$ which -a python python3 pip | xargs -I py py --version
No 'py' such file or folder
Run Code Online (Sandbox Code Playgroud)

但是当我以交互方式运行它时,它们根本不起作用,它甚至不替换字符串。我在手册页中没有看到关于第一个位置的特殊情况的注释。为什么这不起作用?

$ which -a python python3 pip | xargs -I py -p eval py --version
eval ~/.pyenv/shims/python --version?...y
xargs: eval: No such file or directory …
Run Code Online (Sandbox Code Playgroud)

shell xargs fish

5
推荐指数
1
解决办法
250
查看次数

sed 用空格替换换行符

如何使用 sed 将换行符替换为任何其他字符?

\n\n

输入:

\n\n
 I cannot conceive that anybody will    \n require multiplications at the rate of \n 40,000 or even 4,000 per hour ...      \n\n -- F. H. Wales (1936)                  \n
Run Code Online (Sandbox Code Playgroud)\n\n

期望的输出:

\n\n
I cannot conceive that anybody will require multiplications at the rate of 40,000 or even 4,000 per hour ...  -- F. H. Wales (1936)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我试过了:

\n\n
> pbpaste | sed 's/\\n/ /g' \n
Run Code Online (Sandbox Code Playgroud)\n\n

但它输出与输入相同的东西。我知道这是一个换行符,因为我已经检查过它并且cat -ev$按预期打印。

\n\n

还有什么更好的命令可以做到这一点? …

osx terminal sed fish

2
推荐指数
1
解决办法
5585
查看次数

获取文档中每个单词的出现次数

如何查找文件中每个单词的计数?

我想要文本管道或文档中每个单词的直方图。文档中将存在新行和空行。我把除了 之外的所有东西都脱光了[a-zA-Z]

> cat doc.txt 
word second third 

word really
> cat doc.txt | ... # then count occurrences of each word \
                    # and print in descending order separated by delimiter
word 2
really 1
second 1
third 1
Run Code Online (Sandbox Code Playgroud)

它需要具有一定的效率,因为文件是 1GB 文本,并且无法处理指数时间负载。

shell pipe fish macos

2
推荐指数
1
解决办法
1186
查看次数

获取文档中每个单词出现次数的计数/直方图

如何查找文件中每个单词的计数?

我想要文本管道或文档中每个单词的直方图。

我已经能够将文档拆分为单词列表;所以每个单词都换行。如果您可以直接从文本文档中获取它,那么那里的解决方案也很好。

> cat doc.txt 
word
second
third
word
really
> cat doc.txt | ... # then count occurrences of each word \
                      and print in descending order separated by delimiter
word 2
really 1
second 1
third 1
Run Code Online (Sandbox Code Playgroud)

它需要具有一定的效率,因为文件是 1GB 文本,并且无法处理指数时间负载。

text-processing macos

0
推荐指数
1
解决办法
1280
查看次数

标签 统计

fish ×3

macos ×2

pipe ×2

shell ×2

bash ×1

osx ×1

sed ×1

stdout ×1

tee ×1

terminal ×1

text-processing ×1

xargs ×1