Matching multiples of a single character using sed and awk

Hi,

I have a file 'imei_01.txt' having the following contents:

$ cat imei_01.txt
a123456
bbr22135
yet223

where I want to check whether the expression 'first single alphabet followed by 6 digits' is present in the file (here it is the first record 'a123456')

I am using the following script, but it does not give output

sed '/[0-9]\{6\}/p' imei_01.txt

awk '/[a-z][0-9]\{6\}/ {print}' imei_01.txt

For sed this works:

sed -n '/[a-z][0-9]\{6\}/p' infile

Try this:

sed -n '/[A-Za-z]\([0-9]\{6\}\)/p' file
egrep '^[a-z][0-9]{6}$' imei_01.txt

Watch the anchors!

Hi.

If you need to use awk, some versions require a switch to enable the interval expressions:

#!/bin/bash -

# @(#) s1       Demonstrate awk regular expressions.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results, enabling interval expressions:"
awk --re-interval '
/[a-z][0-9]{6}/   {print}
' $FILE

echo
echo " Results, enabling interval expressions, simplified:"
awk --re-interval '
/[a-z][0-9]{6}/
' $FILE

echo
echo " Results, enabling interval expressions, simplified, anchors:"
awk --re-interval '
/^[a-z][0-9]{6}/
' $FILE

exit 0

producing:

% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
GNU Awk 3.1.4

 Data file data1:
a123456
bbr22135
yet223
Not at beginning of line: a123456

 Results, enabling interval expressions:
a123456
Not at beginning of line: a123456

 Results, enabling interval expressions, simplified:
a123456
Not at beginning of line: a123456

 Results, enabling interval expressions, simplified, anchors:
a123456

See man awk for details ... cheers, drl

> cat infile | grep "^[a-zA-Z]"
a123456
bbr22135
yet223

> cat infile | grep "^[a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9]"
a123456
> cat infile | grep "^[a-zA-Z][0-9]\{6\}"
a123456
> grep "^[a-zA-Z][0-9]\{6\}" <infile
a123456