我正在成功地使用以下终端命令在我的非常大的csv文件中找到特定的文本,并将一个单独的csv文件作为输出创建:
grep "text" filename.csv > outputfile.csv是否可以使用类似的命令搜索多个不同的文本,并将其保存在同一个输出文件中?
发布于 2020-04-09 14:02:21
您可以使用-e搜索多个模式:
grep -e text1 -e text2 filename.csv > outputfile.csv使用grep、FreeBSD grep和busybox实现进行测试,也是在POSIX中指定的。-e是如何在grep手册中解释的:
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. If this option is used
multiple times or is combined with the -f (--file)
option, search for all patterns given. This option can
be used to protect a pattern beginning with "-".发布于 2020-04-09 13:46:31
原则上,您可以在正则表达式中使用"OR"-style选项:
grep "text1\|text2" filename.csv > outputfile.csv或
grep -E "text1|text2" filename.csv > outputfile.csv可用的语法将在某种程度上取决于您安装的grep的哪个版本(以上绝对适用于grep)。
发布于 2020-04-09 13:47:16
如果要搜索不同的字符串,可以使用egrep或grep -E:
egrep "text|string|word|" filename.csv > outputfile.csv
grep -E "seal|walrus|otter" filename.csv > outputfile.csv它们将打印出包含任何这些字符串的行。您还可以将它们与其他选项结合起来,例如:
egrep -v "text|string|word|" filename.csv > outputfile.csv它将打印出不包含任何字符串的行。
https://unix.stackexchange.com/questions/578970
复制相似问题