如何解决Shell Delete sed
| 我有一个包含一些数据的文件,如下所示:1,3,0
0,4,5,5
2,6,1,0
我想编写一个Shell脚本来删除第3个参数为0的所有行(此处是第1和2行)
我已经知道了
sed -i \'/0/d\' file.txt
但是我不知道如何选择第三个参数。
你有想法吗?
解决方法
Awk可能比sed更好,可以完成此任务:
awk -F\' *,*\' \'$3 != 0 {print}\' FILE
但是sed可以做到:
sed -i \'/^[0-9][0-9]*,[0-9][0-9]*,/d\' FILE
,sed -iE \'/^([^,]+,){2} 0,/d\' file
用explain.py解释:
sed -iE \'/^([^,/d\' file
\\_/ || ||| || || \\_/ | |
| || ||| || || | | \\- delete command
| || ||| || || | |
| || ||| || || | \\- followed by blank zero komma
| || ||| || || |
| || ||| || || \\- two times this inner pattern
| || ||| || ||
| || ||| || |\\- followed by a comma
| || ||| || |
| || ||| || \\- at least one of them
| || ||| ||
| || ||| |\\- komma
| || ||| |
| || ||| \\- not
| || |||
| || ||\\- start of pattern,group of characters,which are
| || ||
| || |\\- at begin of line
| || |
| || \\- start of pattern for delete command
| ||
| |\\- Extended regexp (parenthesis and braces without masking)
| |
| \\- inplace changes
|
\\- run sed
,使用就地编辑进行awk(类似于sed -i)
awk \'$3!=\"0,\"{print $0>FILENAME}\' file
红宝石(1.9+)
ruby -ane \'print unless $F[2]==\"0,\" \' file
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。