2

I have a file as the following

input file
0.0  4  6  5  1  2  9  4  5  1  ..... 
0.25
0.5  3  1  
1.0  3  7  8  2  
1.5  3  3  3  4  6  4  5   
2.0  
2.5  6  7  6  9 

I want to delete all lines that have only one value in the line so the result will be like this

out file  
0.0  4  6  5  1  2  9  4  5  1  ..... 
0.5  3  1  
1.0  3  7  8  2  
1.5  3  3  3  4  6  4  5   
2.5  6  7  6  9 

3 Answers 3

9

One approach using awk. Uses NF (number of fields) to only print the lines where number of fields is greater than 1.

awk <oldfile >newfile 'NF>1'

Example

awk <oldfile >newfile 'NF>1'
cat newfile
0.0  4  6  5  1  2  9  4  5  1  .....
0.5  3  1
1.0  3  7  8  2
1.5  3  3  3  4  6  4  5
2.5  6  7  6  9
2
  • 1
    will them redirections in between of the arguments work? shouldn't they be after the query?
    – loa_in_
    Commented Jul 15, 2016 at 0:16
  • Yes, see example I've now added. Redirections can be before or after. awk 'NF>1' <oldfile >newfile works equally well, as does awk 'NF>1' oldfile >newfile
    – steve
    Commented Jul 15, 2016 at 8:13
4

For there to be two or more numbers then there must be at least one separator. If this is a space then the grep would simply be

grep ' '

If you file may have extraneous spaces at the end of the line then search for a space followed by a digit or . (in case a number may be .25).

grep ' [0-9.]'

If you may have extraneous spaces at the beginning or end and extra spaces in the middle then

grep '[0-9.]  *[0-9].'
2

You can do this with grep or sed.

With sed:

sed -e '/^[[:space:]]*[[:digit:].]\+[[:space:]]*$/d' input >output

With grep, use the -E option for regular expressions, and -v option to exclude matches:

grep -v -E '^[[:space:]]*[[:digit:].]+[[:space:]]*$' input >output

The reason for the pattern is to ignore lines which do not have a numeric value, and to handle cases with leading or trailing blanks. If none of that is of interest, a simpler solution "works".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .