Remove a line if it contains more than 3 spaces
I have a text file in the form
sadsadsad sadsadsadd sadasdsadad
some of the lines in there contain an addition space in the middle of the s开发者_运维知识库econd column such as
sadsadsad sads adsadd sadasdsadad
Can I use awk to look at the file and delete the line if there's more than 2 spaces on a particular line?
You don't need awk
for that, a simple sed
will do:
sed -e '/ .* .* /d'
If you want awk
specifically, you can do something like this:
!/ .* .* / {
print;
}
You can also customize the regexp to be more precise, maybe by using in both cases / [^ ]+ [^ ]+ /
. Or even, as jkerian proposes in another answer, just use NF
.
There's probably a regex you can create to do this with a regex match with sed or awk... but this works
awk '{if ($4 == "") print}' input_file
Another alternative is to use the NF variable
awk '{if (NF < 4) print}' input_file
精彩评论