2

Consider a text.txt file having the form

25 1 4 5 2 19 38

21 0 8 8 7 7 61

<clus scale = "125"> 1 3 2</clus>

25 3 1.63 123 56 12 38

21 123 12.3 12.1 1.5 2.67 3.77

<clus scale = "227"> 5 6 2</clus>

How can I leave only numeric (and empty) rows using the terminal, i.e. to convert a file to

25 1 4 5 2 19 38

21 0 8 8 7 7 61

25 3 1.63 123 56 12 38

21 123 12.3 12.1 1.5 2.67 3.77
  • How do you define "numeric"? Your output also has empty lines. Do you want to keep all lines that have only numbers and whitespace and all empty lines? – terdon Apr 23 at 8:21
  • @terdon : yes, that's what I would like to have. – John Taylor Apr 23 at 8:22
6

Use grep:

grep '^[0-9. ]*$' text.txt

or to also exclude empty lines from the match, use

grep -E '^[0-9. ]+$' text.txt

This will select lines with numbers, dot and space.
You can also reverse the pattern, by excluding lines with for example < using grep -v:

grep -v "<" text.txt
  • 2
    Note that your second grep won't exclude lines with only whitespace, it will only exclude lines with no characters. And you might want to include - for negative numbers. – terdon Apr 23 at 8:26

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.