Regular expression for 6 digit number present in a line

Hello Team,
i have a file test1.txt, in which i have to grep only the 6 digit number from it,
Could you pls help in this.

$cat test1.txt
  <description>R_XYZ_1.6 r370956</description>
$ grep "[0-9]\{6\}" test1.txt
  <description>R_XYZ_1.6 r370956</description>

i need output as 370956.

Regards,
Chandana

if you're using gnu grep, grep -o returns the matched text.
if you don't have gnu grep available try, perl -ne 'print "$1\n" if /(\d{6})/' test1.txt

Thanks,

Got it.

$ cat test1.txt |sed -n -e 's/.*\([0-9]\{6\}\).*/\1/p'
370956

using awk

awk '{match($0,"[0-9][0-9][0-9][0-9][0-9][0-9]",a);print a[0]}' test1.txt
370956

PS do not cat the file to sed/awk , add file behind, like my example.

1 Like