[英]Copy a line containing a string to another file in shell script
I am new to shell scripting and I am trying to figure out the following. 我是shell脚本的新手,我想弄清楚以下内容。
How to search a file for a line containing a particular string and copy that line to a new file if the line is present? 如何在文件中搜索包含特定字符串的行,如果该行存在,则将该行复制到新文件中?
grep "string" file >copyFile
This would create a new file or empty an existing file. 这将创建一个新文件或清空现有文件。
grep "string" file >>copyFile
This would create a new file or append found string to an existing file. 这将创建一个新文件或将找到的字符串附加到现有文件。
Nota: "string"
is a regex . Nota:
"string"
是一个正则表达式 。 Take a look at man grep
for syntax. 看一下
man grep
的语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.