【问题标题】:Bash uniq taking newline into account考虑换行符的 Bash uniq
【发布时间】:2014-03-02 18:50:29
【问题描述】:

由于某种原因,uniq 似乎在比较不同行时识别换行符,并且识别文件换行符与 \n 不同(或者至少在我看来是这样)。

我想要它做的是这样的文件:

hello world hello hello
meh
hello
hello

并获得如下输出:

5 hello
1 world
1 meh

但是我的代码:

x=`sed 's/ /\n/g' $1 | uniq -c | sed 's/      //g'`
echo "$x"

正在输出这个:

1 hello
1 world
2 hello
1 meh
2 hello

【问题讨论】:

  • “出于某种原因”?为什么会有这样的惊喜?来自手册页:uniq - report or omit repeated lines

标签: bash line newline uniq


【解决方案1】:

uniq 需要排序的输入。

$ sed 's/ /\n/g' f | sort | uniq -c | sed 's/      //g'
5 hello
1 meh
1 world

【讨论】:

    猜你喜欢
    • 2013-08-14
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多