简体   繁体   English

使用/删除文本文件中包含特定字符串的行

[英]Delete lines in a text file that contain a specific string with /

I have a text file with dates in this format: 27/8/2019 I would like to remove all lines except for those that contain 27/8/2019 我有一个文本文件,其日期格式为:27/8/2019我想删除除包含27/8/2019的行以外的所有行

If I use sed, that would be: 如果我使用sed,那将是:

sed -i '/pattern/!d' file.txt

The problem is that when I have a pattern with '/', I have the next error: 问题是,当我使用'/'模式时,出现下一个错误:

sed: -e expression #1, char 5: unknown command: `8'

转义斜线:

sed -i '/27\/8\/2019/!d' file.txt

Using awk 使用awk

awk '/27\/8\/2019/' file.txt

If date is just a sample, use this to keep all date line: 如果日期只是一个示例,请使用此选项保留所有日期行:

awk '/[0-9]+\/[0-9]+\/[0-9]+/' file.txt

To update file inline with awk 使用awk内联更新文件

awk '-your code-' file.txt > tmp && mv tmp file.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从文本文件中删除包含特定文本的行 - remove lines from text file that contain specific text 计算文本文件中包含特定字符的行数(Linux)? - Count the number of lines in a text file that contain a specific character (Linux)? 正则表达式使用grep查找不包含特定字符串的文本行(linux terminal-opensuse) - Regular expression to find the text lines that do not contain a specific string using grep(linux terminal-opensuse) Linux:删除不包含特定行数的文件 - Linux: delete files that don't contain specific number of lines 如何在大型文本文件中选择包含特定文本字符串的所有行 - How to select all rows in a large text file that contain a specific text string 删除日志文件中特定日期之前的行 - Delete lines prior to a specific date in a log file 在 Linux 上显示不包含特定字符串的行 - Show lines which does not contain specific string on Linux 如果接下来的X行不包含特定字符串,则使用grep - grep if the next X lines doesn't contain a specific string 在bash / linux中,尝试从文件中满足特定条件的文件中删除以特定字符串开头的行 - Trying to delete lines beginning with a specific string from files where the file meets a target condition, in bash/linux 查找和删除文本文件中包含多个单词的行 - Find and delete lines with multiple words in a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM