How to filter out consecutive occurence of digits from file?

Hello,

I have a text file containing the following for example:

This number 123456 is in line number one.
Line number two contains -333444 number.
The third line has a 111111 in between and also 435.
Line number four has :444444 in it.
Line five has a in front like a555555.

how can i filter out all the six-digit numbers in the text i.e the required output is:

123456
333444
111111
444444
555555

Thanks

Hello james2009,

Could you please try following and let me know if this helps you. It will remove all 6 consecutive digits from each line.

awk  '{gsub(/[0-9]{6}/,X,$0);print}'   Input_file

EDIT: If you need to get as output from Input_file for all 6 consecutive digits then following may help you in same.

awk '{match($0,/[0-9]{6}/);print substr($0,RSTART,RLENGTH)}'   Input_file

Thanks,
R. Singh

Hi ravinder,

Thanks but it didn't work.

I think the OP wants to see the numbers (works only with GNU tr command on Linux):

 awk  '{ for(i=1;i<=NF; i++)
          if( $(i) ~ /[0-9]{6}+/)
          {
            printf("%s\n", $(i) )
          }
       }'  filename | tr -dc '[[:digit:][:cntrl:]]'
123456
333444
111111
444444
555555

Hi.

Also:

grep -E -o '[0-9]{6}' <file>

producing:

123456
333444
111111
444444
555555

on a system:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
bash GNU bash 4.3.30
grep (GNU grep) 2.20

Best wishes ... cheers, drl

1 Like

Thanks drl. It worked.