Quantcast
Channel: Grep for a line containing only 5 or 6 numbers - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 8

Answer by user78605 for Grep for a line containing only 5 or 6 numbers

$
0
0

A way with awk that is customisable for different numbers of fields.
Also whitespace does not matter.

awk 'NF~/^(5|6)$/{x=0;for(i=1;i<=NF;i++)x+=($i~/^[0-9]+$/)}x==NF' file

This checks the number of fields is 5 or 6 although more numbers of fields could be added if your requirements ever change.

Then it sets a counter to 0

Then loops checking each field is a number and if it is adds 1 to the counter

If the counter equals the number of fields it prints the line.

example

input

  1       2       3       4       5        6
        2       3       4       5        6
3       4       5        6
blah  1       2       3       4       5
      4       3324       4       53        6

output

  1       2       3       4       5        6
        2       3       4       5        6
      4       3324       4       53        6

Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>