1823178 Members
3890 Online
109647 Solutions
New Discussion юеВ

Re: grep command

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

grep command

Hi All,

Help me please. I want to grep from a file where the first digit must be lower case character a-z, the second digit can be lower character or none, and followed by 4 digit number.

Example, a file called file1 contents :
a1234
aa1234
syssupport1
ac1511
aducate1234

using grep command,the result must be :
a1234
aa1234
ac1511
ap1030

Please help.

Thanks and Best Regards,
Negara


Santos
7 REPLIES 7
Vijaya Kumar_3
Respected Contributor
Solution

Re: grep command


grep "^[a-z][a-z1-9][1-9][1-9][1-9][1-9]" filename
Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
Dewa Negara_4
Regular Advisor

Re: grep command

Thanks. It is working well.

Thanks and Best Regards,
Negara
Santos
Steve Steel
Honored Contributor

Re: grep command

Hi

cat file|
while read line
do
echo $line|grep ^[a-z][a-z][0-9][0-9][0-9][0-9]
echo $line|grep ^[a-z][0-9][0-9][0-9][0-9]
done

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Sanjay Kumar Suri
Honored Contributor

Re: grep command

Check this

grep ^[a-z][a-z][0-9][0-9][0-9][0-9] file_nm

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Dewa Negara_4
Regular Advisor

Re: grep command

Thanks everyone. All those are fine. Thanks for the great help.

Best Regards,
Negara
Santos
karthika
New Member

Re: grep command

Hi,

The above said grep commands are not exact.Take for example your file is like this.
a1234
aa1234
syssupport1
ac1511
aducate1234
a123456

Executing the above commands results in:
a1234
aa1234
ac1511
a123456 -- (this is not our desired output)

so, better try this.
cat filename | grep '^[a-z]*[a-z][0-9][0-9][0-9][0-9]$'

Thank you,
karthika
H.Merijn Brand (procura
Honored Contributor

Re: grep command

# perl -ne'/^[a-z][a-z]?\d{4}\b/ and print' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn