9

I have a file as the following

  200.000    1.353    0.086
  200.250    1.417    0.000
  200.500    1.359    0.091
  200.750    1.423    0.000
  201.000    1.365    0.093
  201.250    1.427    0.000
  201.500    1.373    0.093
  201.750    1.432    0.000
  202.000    1.383    0.091
  202.250    1.435    0.000
  202.500    1.392    0.087
  202.750    1.436    0.000
  203.000    1.402    0.081
  203.250    1.437    0.001
  203.500    1.412    0.073
  204.000    1.423    0.065
  204.500    1.432    0.055
  205.000    1.441    0.045  

I would like to grep only the rows that have in the first column the decimal .000 and .500 only so the output would be like this

  200.000    1.353    0.086
  200.500    1.359    0.091
  201.000    1.365    0.093
  201.500    1.373    0.093
  202.000    1.383    0.091
  202.500    1.392    0.087
  203.000    1.402    0.081
  203.500    1.412    0.073
  204.000    1.423    0.065
  204.500    1.432    0.055
  205.000    1.441    0.045  
  • 2
    It looks easy enough. What have you tried so far? What problems did your code have? – John1024 Oct 31 '16 at 21:26
  • maybe it is easy for you but I tried with grep '.000' | grep '.005' but it also sorts the rows that have the same value in other columns – Mohsen El-Tahawy Oct 31 '16 at 21:36
  • 3
    Very good. People here are much more sympathetic if you show an honest attempt to solve the problem yourself. The code in your comment shows that. In the future, if you include attempts like that in your question, you will likely get better responses faster. – John1024 Oct 31 '16 at 22:07
14

You don't use grep. Use awk.

"your data" | awk '$1 ~ /\.[05]00/'
  • Very good. As written, the code depends on there being exactly three digits after the decimal. It would be more robust to use awk '$1 ~ /\.[05]0*$/'. – John1024 Oct 31 '16 at 22:17
  • 1
    @John1024, actually as written the code depends on there being at least three digits after the decimal. I would incline toward awk '$1 ~ /\.[05]00$/', myself (require exactly three digits), unless I had reason to think that variable decimal places are expected in the input. – Wildcard Oct 31 '16 at 23:22
  • 2
    @Wildcard If there are more than three, the code may fail. For example: echo 0.5001 | awk '$1 ~ /\.[05]00/'. It only works reliably if there are exactly three. – John1024 Oct 31 '16 at 23:34
4
awk '$1 ~ /\.[50]00/ { print $0 }' myFile.txt

The first column $1 will be matched against /\.500|\.000/ the dots are escaped to be literal dots not regex any character the ~ is partial match, and print the whole line $0

  • 2
    No reason to include { print $0 }; that is Awk's default action. – Wildcard Oct 31 '16 at 23:20
4

I would like to grep only the rows that have in the first column the decimal .000 and .500

My first thought

grep '^ *[0-9][0-9][0-9]\.[50]00' filename

Quick test using WSL

$ head testdata
              200.000    1.353    0.086
              200.250    1.417    0.000
              200.500    1.359    0.091
              200.750    1.423    0.000
              201.000    1.365    0.093
              201.250    1.427    0.000
              201.500    1.373    0.093
              201.750    1.432    0.000
              202.000    1.383    0.091
              202.250    1.435    0.000
$ grep '^ *[0-9][0-9][0-9]\.[50]00' testdata
              200.000    1.353    0.086
              200.500    1.359    0.091
              201.000    1.365    0.093
              201.500    1.373    0.093
              202.000    1.383    0.091
              202.500    1.392    0.087
              203.000    1.402    0.081
              203.500    1.412    0.073
              204.000    1.423    0.065
              204.500    1.432    0.055
              205.000    1.441    0.045

There are more concise ways to express this.

$ grep -E '^ *[0-9]{3}\.[50]00' testdata
              200.000    1.353    0.086
              200.500    1.359    0.091
              201.000    1.365    0.093
              201.500    1.373    0.093
              202.000    1.383    0.091
              202.500    1.392    0.087
              203.000    1.402    0.081
              203.500    1.412    0.073
              204.000    1.423    0.065
              204.500    1.432    0.055
              205.000    1.441    0.045

If the first column may have other than a 3-digit integer part

grep -E '^ *[0-9]+\.[05]00' testdata

Under some circumstances you might need to use [:digit:] in place of [0-9].

And so on.

man grep is your friend.

  • This usage of grep is easier to use than mine. I wouldn't have posted an answer should I have seen this first. Nice job! – Yokai Nov 1 '16 at 8:22
2

Depending on your use case, you might also use actual numeric operations:

$ awk '{a = $1 % 1} a == 0 || a == 0.5' /tmp/foo
  200.000    1.353    0.086
  200.500    1.359    0.091
  201.000    1.365    0.093
  201.500    1.373    0.093
  202.000    1.383    0.091
  202.500    1.392    0.087
  203.000    1.402    0.081
  203.500    1.412    0.073
  204.000    1.423    0.065
  204.500    1.432    0.055
  205.000    1.441    0.045

Tested with BSD awk (OSX El Capitan, 20070501) and GNU awk 4.1.4.

  • 1
    Warning: testing exact equality of floating-point (which awk uses) often gives 'wrong' results unless the values have no fractional part (and are not too large in magnitude), or the fractional part is 'binary' (exactly half, a quarter, etc) which is true for the data in this Q but not many others that appear similar to the uninitiated. – dave_thompson_085 Nov 1 '16 at 9:00
  • 1
    @dave_thompson_085 indeed, but with gawk you can use arbitrary precision arithmetic, admittedly I'm not using them here. – muru Nov 1 '16 at 9:17
2
 grep -e '2[^ ]*.000' -e '2[^ ]*.500' file.txt
2

With awk:

$>awk '$1%.5==0' data.tsv 
200.000 1.353   0.086
200.500 1.359   0.091
201.000 1.365   0.093
201.500 1.373   0.093
202.000 1.383   0.091
202.500 1.392   0.087
203.000 1.402   0.081
203.500 1.412   0.073
204.000 1.423   0.065
204.500 1.432   0.055
205.000 1.441   0.045

With mlr:

$>mlr --ifs tab --onidx filter '$1%.5==0' data.tsv 
200.000 1.353 0.086
200.500 1.359 0.091
201.000 1.365 0.093
201.500 1.373 0.093
202.000 1.383 0.091
202.500 1.392 0.087
203.000 1.402 0.081
203.500 1.412 0.073
204.000 1.423 0.065
204.500 1.432 0.055
205.000 1.441 0.045
2

Ok, a little late adding in my contribution, but I think it's worth it.

The requirement to meet, per the OP is the first column having the decimal value of .000 or .500 only. There's no stipulation as to the leading value, either by range or length. For robustness it shouldn't be assumed to be constrained by anything except that there are no non-blank characters before the first column (or it's no longer the first column) and that the contents of the first column will have a decimal point,., in it somewhere.

The OP is wanting to use grep, which will print the whole line when a match is found, so the only thing to do is create the pattern that matches all and only what is required.

Simplicity itself, and no reason to use sed or awk as `grep can handle the source as a file or a pipe.

To grep a file use grep '^[^.]*\.[05]0\{2\}\s' the_file.txt

To grep from a pipe, use my_command | grep '^[^.]*\.[05]0\{2\}\s'

The pattern is: ^, start at the beginning of the line; [^.], match any non-decimal character; *, as many times as possible (including none); \., match a decimal point; [05], match either a five or a zero; 0\{2\}, match 2 more zeros (the backslashes before the open and close brace prevent the shell from trying to do brace expansion); \s, match a whitespace character (meaning the end of the column - to use in a different use case, replace with the column separator, typically a comman, a semi-colon, or a tab \t).

Note that this will match exactly what the OP asked. It will not match .5000 or .0000 even though numerically equivalent, because the pattern looks for a five or a zero, followed by exactly 2 more zeros followed by whitespace. If that is significant, then all other answers, so far, fail in that they will match any number of zeros, greater than 1, after the test digit. And except to the answer by FloHimself, they will match anything in the second column that begins .000 or .500, including .0003 and .500T, and the one by FloHimself will match anything that is mathematically equivalent to .0 and .5, no matter how many zeros there are. The last one, while not matching what the OP stated is likely to match what the OP needs anyway.

Finally, if the power, and speed, of awk is desired, even though the OP asked for grep, then the command would be:

With a file awk '$1 ~ /[^.]\.[05]0{2}$/' the_file.txt

With a pipe my_command | awk '$1 ~ /[^.]\.[05]0{2}$/'

1

If you insist on using grep, then this may work for you. I saved the first output you provide to a text file called, "file.txt" and then used the following command:

grep -e '2[^ ]*.000' file.txt & grep -e '2[^ ]*.500' file.txt

Which gives an output of:

200.000    1.353    0.086
200.500    1.359    0.091
201.500    1.373    0.093
201.000    1.365    0.093
202.500    1.392    0.087
202.000    1.383    0.091
203.500    1.412    0.073
203.000    1.402    0.081
204.500    1.432    0.055
204.000    1.423    0.065
205.000    1.441    0.045

You won't have to save the output to a text file if it is already in a file. But in case it is not being saved to a file, you can also pipe the data into the grep command I provided and it should work at least until the very first number, 2, in the first column is no longer a 2. At that point you will need to update the grep command with the appropriate character to print correctly.

What is happening with this dual grep command is that the first grep is being sent to the background with the & operator. As it is sent to background, the next grep command executes immediately afterwards giving you a uniform output. For the task you need completed to be done more easily, you should follow the example that others have given and use awk or even sed.

(edit)

This is by no means the best or most effective usage of grep for your needs but it should be sufficient enough for you to play around a bit and get a better feel for grep.

  • The first process does run in background, but not daemonized which includes running in background but quite a bit more. And it is very unlikely to produce output in the same order as the input; even in your quite small example it's already gone wrong at the third line. – dave_thompson_085 Nov 1 '16 at 9:04
  • He doesn't mention that the output need be in a specific order. Only that it need to be specific to the .500 and .000 of the first column. If it need be in a specific order, such as least to greatest, that can easily be done. However, the first 3 digits of the first columns being printed are in least to greatest order. That is the result of the 2[^ ]*.000 and 2[^ ]*.500. Its quite fitting to what the OP asked. – Yokai Nov 1 '16 at 9:20
  • Also note my edit for efficiency disclaimer for the command I provided. – Yokai Nov 1 '16 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.