Unix将grep命令重定向到文件

时间:2019-11-16 19:38:29

标签: unix grep command

对于作业,我必须执行以下操作:'抓取所有以大写字母开头的行,这些行仅在“ test1.txt”中包含字母(大写或小写)。重定向此命令的输出以附加到现有的“ output2.txt”。'

我尝试使用

grep ^[A-Z]*[a-z] > test1.txt >> output2.txt

但是它给我一个错误,提示“歧义输出重定向”。我仍在学习如何使用Unix,所以不确定如何修复它。

2 个答案:

答案 0 :(得分:0)

为避免进行分配,我将举一个简单的示例说明如何使用cat进行输出重定向。您可以翻译为grep

$ # Print file to standard output
$ cat test1.txt
abc
Abc
9B
$ # Print to another file, overwriting existing file
$ cat test1.txt > output2.txt
$ cat output2.txt
abc
Abc
9B
$ # Print to another file, appending to existing file
$ cat test1.txt >> output2.txt
$ cat output2.txt
abc
Abc
9B
abc
Abc
9B

答案 1 :(得分:0)

命令应如下所示:

egrep "^[A-Z][a-zA-Z]*$"  test1.txt > output2.txt

要使用正则表达式,应使用egrep。然后,您将第一个符号搜索为大写字符,然后将下一个仅搜索为字符,因此正则表达式应如下所示。然后yoy应该使用test1.txt作为输入文件(您将其设置为从STDOUT重定向),然后就不需要追加了,因为整个命令将同时输出所有字符串