Shell Delete sed
I have a file with some data like this:
1, 3, 0, 0, 0
0, 4, 5, 0, 5
2, 6, 0, 1, 0
I would like to write a shell script to delete all the lines where the 3rd argument is 0 (here line 1 and 2)
I already know that
sed -i '/0/d' file.txt
but I don't know how I can select the 3rd arguments whatever the othe开发者_StackOverflow社区rs one.
Do you have an idea?
Awk may be a better tool than sed for this task:
awk -F' *, *' '$3 != 0 {print}' FILE
But sed can do it:
sed -i '/^[0-9][0-9]*, [0-9][0-9]*, 0,/d' FILE
sed -iE '/^([^,]+,){2} 0,/d' file
explained with explain.py:
sed -iE '/^([^,]+,){2} 0,/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 with in place editing (similar to sed -i)
awk '$3!="0,"{print $0>FILENAME}' file
Ruby(1.9+)
ruby -ane 'print unless $F[2]=="0," ' file
精彩评论