简体   繁体   English

在bash脚本中结合sed和sort命令

[英]combine sed and sort commands in bash script

I have this bash commands to filter words in a text file (file1 in the example) 我有这个bash命令来过滤文本文件(示例中的file1)中的单词

Until now i have to use two separate commands to get the result i want 到现在为止,我必须使用两个单独的命令来获得所需的结果

sed -n "SAMPLETEXT" file1 > file2
sort file2 | uniq -c > file2.tmp && mv file2.tmp file2.txt 

because i need to filter out lines with certain strings from file1 and then count all equal lines. 因为我需要过滤掉file1中某些字符串的行,然后计算所有相等的行。

is there a way to do that all in one command to show the output in the console so that i dont need to even create a "file2"? 有没有一种方法可以在一个命令中完成所有操作以在控制台中显示输出,因此我什至不需要创建“ file2”?

sed -n "SAMPLETEXT" file1|sort| uniq -c

您也可以使用grep:

grep "SAMPLETEXT" file1 | sort | uniq -c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM