LINUX Shell 命令 cat 和 grep

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16961084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:08:48  来源:igfitidea点击:

LINUX Shell commands cat and grep

linuxshell

提问by Vignesh T.V.

I am a windows user having basic idea about LINUX and i encountered this command:

我是一个对 LINUX 有基本概念的 Windows 用户,我遇到了这个命令:

cat countryInfo.txt | grep -v "^#" >countryInfo-n.txt

After some research i found that cat is for concatenation and grep is for regular exp search (don't know if i am right) but what will the above command result in (since both are combined together) ?

经过一些研究,我发现 cat 用于串联,而 grep 用于常规 exp 搜索(不知道我是否正确)但是上面的命令会导致什么结果(因为两者结合在一起)?

Thanks in Advance.

提前致谢。

EDIT: I am asking this as i dont have linux installed. Else, i could test it.

编辑:我问这个是因为我没有安装 linux。否则,我可以测试它。

采纳答案by DarkDust

Short answer: it removes all lines starting with a #and stores the result in countryInfo-n.txt.

简短回答:它删除所有以 a 开头的行#并将结果存储在countryInfo-n.txt.

Long explanation:

长解释:

cat countryInfo.txtreads the file countryInfo.txtand streams its content to standard output.

cat countryInfo.txt读取文件countryInfo.txt并将其内容流式传输到标准输出。

|connects the output of the left command with the input of the right command (so the right command can read what the left command prints).

|将左命令的输出与右命令的输入连接起来(因此右命令可以读取左命令打印的内容)。

grep -v "^#"returns all lines that do not(-v) match the regex ^#(which means: line starts with #).

grep -v "^#"返回所有( -v) 匹配正则表达式的行^#(这意味着:行以 开头#)。

Finally, >countryInfo-n.txtstores the output of grepinto the specified file.

最后,>countryInfo-n.txt将 的输出存储grep到指定的文件中。

回答by shyam

It will remove all lines starting with #and put the output in countryInfo-n.txt

它将删除所有以开头的行#并将输出放入 countryInfo-n.txt

回答by devnull

This command would result in removing lines starting with #from the file countryInfo.txtand place the output in the file countryInfo-n.txt.

此命令将导致#从文件中删除以 开头的行countryInfo.txt并将输出放在文件中countryInfo-n.txt

This command could also have been written as

这个命令也可以写成

grep -v "^#" countryInfo.txt > countryInfo-n.txt

See Useless Use of Cat.

请参阅Cat 的无用使用