0

I have a file with something like this:

sda             204.98         0.00    104947.26          0     210944
sda             205.50         0.00    104458.00          0     208916
sda             212.50         0.00    108800.00          0     217600
sda             225.00         0.00    114698.00          0     229396
sda             207.00         0.00    105728.00          0     211456
sda             187.00         0.00     94988.00          0     189976
sda             373.50      3704.00      2248.00       7408       4496
sda             419.50      4152.00      2508.00       8304       5016
sda             390.50      3696.00      2506.00       7392       5012

And I need with a bash script to delete all the rows like the first three, where the third and fifth columns contains zero values, or in alternative, where the last column has high values (more than 5 digits), in order to obtain:

sda             373.50      3704.00      2248.00       7408       4496
sda             419.50      4152.00      2508.00       8304       5016
sda             390.50      3696.00      2506.00       7392       5012

I've tried with awk '($6 < .....)' but has no effect.

How can I do that?

0

2 Answers 2

3

It's easily done using awk. Try the following script :

awk -v max=100000 '$3 && $5 && $6 < max' data

This ensures fields 3 and 5 are non-zero ($3 is a shorthand for $3 != 0), and that the last field has 5 or less digits.

As awk's default behaviour is to print the whole line, this will output all the lines satisfying this condition.

Sign up to request clarification or add additional context in comments.

5 Comments

Why would you want to answer when OP has absolutely shown no efforts of any attempts and just asks for code for me? Do not encourage doing this. I purposely deleted the answer and asked a fellow poster to do the same to improve this question.
The answer was updated meanwhile, and is to me at least clear.
@setempler: Not sure what you mean. I wanted to maintain the ethics of the community by asking the OP to post his research attempt before posting answers which clearly Aserre violated.
@setempler: I can of-course go and do a blind downvote on all provided answers but that is not the right thing to do!
@Inian FYI, this issue has been discussed multiple time on Meta/SO Meta. This question is the most viewed/upvoted I was able to find on the subject. The point is, asking everytime "what have you tried so far" is counter-productive and leads to "Please debug my code" questions with unformated code dumps. Here, the OP clearly stated his issues, described a specific behaviour and added the relevant desired input/output, so I answered his question.
2

You can use awk in a bash script, like:

awk '($3 > 0 && $5 > 0) {print}' file

or

awk '($3 != 0 && $5 != 0) {print}' file

or according to your edit (minimum value in last column):

awk '($3 != 0 && $5 != 0) || ($6 > 9999) {print}'

Where

  • $3 and $5 are 3rd and 5th column variables,
  • and the script checking if values in that column are > 0 (larger than 0) or != 0 (not equal 0), then
  • it prints the line of the file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.