2
votes

I would like to delete the lines from text files if $4 is one

123  34  A   0
23   45  A   1  
36   5   A   36
176  3   A   1

desired output

123  34  A   0
36   5   A   36

I need in-place editing. How can I do this with awk or sed?

4

4 Answers

2
votes

One way using GNU sed with -i for in-place editing and -r for extended regular expressions:

sed -ri '/^\S+\s+\S+\s+\S+\s+1( |$)/d' file

If you desperately need in-place editing, you may also like to try perl with it's auto-split functionality. The code is much more readable and portable too:

perl -i -ane 'print if $F[3] != 1' file

Results:

123  34  A   0
36   5   A   36
5
votes

In awk:

awk '$4 != 1'

It is doable in sed, but sufficiently much harder that I'd not bother:

sed '/^[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*  *1 *$/d'

Or, if you've got GNU sed:

sed -r '/^[^ ]+ +[^ ]+ +[^ ]+ +1 *$/d'
1
votes

Just to be different...

$ ed << \eof
g- 1$-d
w
q
eof
0
votes

with awk

awk '{if($4 != 1 ) print $0}' temp.txt