如何使用 uniq -c 选项进行自定义格式化?
来自维基百科:
uniq
-c 以默认样式生成输出报告,只不过每行前面都有其发生的次数计数。如果指定此选项,则 -u 和 -d 选项中的一个或两个都存在时将被忽略。
在我的机器上,它获取计数并将其放在每行的开头。我想要的是将其放置在行尾、逗号之后。这怎么能做到呢?
示例:
aa
aa
bb
cc
cc
dd
应更改为:
aa,2
bb,1
cc,2
dd,1
From wikipedia:
uniq
-c Generate an output report in default style except that each line is preceded by a count of the number of times it occurred. If this option is specified, the -u and -d options are ignored if either or both are also present.
On my machine it is taking the count number and putting it on the start of each line. What I want is for it to be placed at the end of the line, after a comma. How can this be done?
Example:
aa
aa
bb
cc
cc
dd
Should change to:
aa,2
bb,1
cc,2
dd,1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试这样的操作 -
或
或
或
测试:
测试2:
测试3:
You can try something like this -
or
or
or
Test:
Test2:
Test3:
像这样简单的事情,
sed
比awk
更容易uniq -c inputfile.txt | sed -e 's/^ *\([0-9]\+\) \(.\+\)/\2,\1/'
Simple things like this,
sed
is easier thanawk
uniq -c inputfile.txt | sed -e 's/^ *\([0-9]\+\) \(.\+\)/\2,\1/'
我会使用
awk
因为我发现它最具可读性I'd use
awk
as I find it most readable