简体   繁体   English

将包含字符串的行复制到shell脚本中的另一个文件

[英]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.

相关问题 Linux Shell脚本:将文件和字符串一起写入另一个文件 - Linux Shell Script: Writing file and string together to another file 删除外壳中包含特定字符串的行 - delete line containing a specific string in shell 将一个 substring 替换为 shell 脚本中的另一个字符串 - Replace one substring to another string in shell script 从一行复制一个字符串并在另一行的末尾粘贴一个基于模式的大文件 - copy a string form a line and paste at the end in another line for a huge file based on pattern 查找包含字符串的行并用新字符串替换另一个字符串 - Find line containing string and replace another string with new string 在文件名中查找字符串(shell脚本) - Find a string in a file name (shell script) 使用Shell脚本从文件中提取字符串 - extract string from a file using shell script Shell脚本解析/检索在另一个字符串/匹配项之后找到的字符串 - Shell Script to parse/retrieve a string found after another string/match 如何在文件中搜索字符串并打印包含该字符串的行? - How to search for a string in a file and print the line containing that string? 将行复制/写入到另一个文件上,但对该行进行较小的更改 - Copy/write line on to another file with minor change on the line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM