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