Reputation: 683
I'd like to find a way to delete lines containing more than 4 digits using awk or sed:
input:
test12test1
test154test43test
test12
desired output:
test12test1
test12
How do I solve this problem?
Upvotes: -3
Views: 360
Reputation: 41456
One of these may do.
awk -F '[0-9]' 'NF<=5' file
test12test1
test12
awk -F '[0-9]' 'NF<6' file
test12test1
test12
Set the field separator to any possible decimal digit and then perform a test on the total number of fields.
Since you get a field before and after the field-separator, you need to test if the line more than 5 fields to determine if the line has more than 4 decimals.
Upvotes: 5
Reputation: 58400
This might work for you (GNU sed):
sed 's/[0-9]/&/4;T;d' file
If one can replace the 4th digit, then delete the line.
Upvotes: 1
Reputation: 246807
The perl tr///
operator returns the number of substitutions, so
perl -ne 'print if tr/0-9/0-9/ <= 4' file
Or if you want to use a shell variable
max_digits=4
perl -sne 'print unless tr/0-9/0-9/ > $max' -- -max=$max_digits file
Upvotes: 3