awk regex problem

hi everyone

suppose my input file is

 
ABC-12345
ABCD-12345
BCD-123456

i want to search the specific pattern which looks like

[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9][0-9]

in a file so i used this command

cat $file | awk ' { if ($0 ~ /[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9][0-9]/) { print } }'

so it gives me the result as

 
ABCD-12345
BCD-12345
BCD-12345

but this is not correct i want the exact string from this
so output should be only

BCD-12345

please help me in this

grep "^[A-Z]\{3\}\-[0-9]\{5\}" filename

hi thanks
for reply

but can u please tell me the solution in awk because i have to implement it from awk only .. because output from many command is fed to that ( proposed awk command)

awk ' /^[A-Z]\{3\}\-[0-9]\{5\}/ {print}' filename
nawk '$0~/^[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9][0-9]$/' inputfile
ABC-12345

Thanks
SHa

hi thanks all of u for ur reply

can u please tell why the below code is failing

echo ABCD-12345 | grep '[^A-Z][A-Z]\{4\}-[0-9]\{5\}'

and it returns nothing

It is expecting 1+4 characters and followed by hypen(-) and 5 numbers

[^A-Z][A-Z]\{4\}

hii
many many thanks for reply

so hw can we avoid this problem see in my file data can contain like this

 
 
ABCDE-12345
abc BCDA-12345
defe erjhj ABCD-12345
dfjfh sdhjh jkfgh dfj aaaAAAA-12345

in that i have to find exact

 
BCDA-12345
ABCD-12345

---------- Post updated at 02:38 AM ---------- Previous update was at 02:37 AM ----------

and also i forgot to add

 
AAAA-12345
awk ' /[A-Z]\{4\}\-[0-9]\{5\}/ {print}' filename

is the above code is not working ?

You should keep the ^ out of [A-Z] and change 4 to 3 as

 echo ABCD-12345 | grep '^[A-Z][A-Z]\{3\}-[0-9]\{5\}'

@above

thanks for ur reply

but the pattern

ABCD-12345

can come anywhere in code and also it can be preceeded with any character between [A-Z]

so i can't use ur solution

i tried this sol'n too

 
cat test2
ABCDE-12345
abc BCDA-12345
defe erjhj ABCD-12345
dfjfh sdhjh jkfgh dfj aaaAAAA-12345

 
awk ' /[A-Z]\{4\}\-[0-9]\{5\}/ {print}' test2

it too returns nothing..

awk ' /[A-Z]\{4\}\-[0-9]\{5\}/ {print $0}' test2

what linux favour you are using ?

@above

i am using solaris also i am not directly fetching from file
i am giving line by line to another awk statement which would then extract the pattern

so please help in this case.

please post your orginal code and you need only the below text from the "dfjfh sdhjh jkfgh dfj aaaAAAA-12345"

AAAA-12345

Try this.

cgi@tonga> (/home/cgi) $ gawk    --posix '/[A-Z][A-Z]{3,4}-[0-9]{5}/ {print}' test.txt
ABCDE-12345
abc BCDA-12345
defe erjhj ABCD-12345
dfjfh sdhjh jkfgh dfj aaaAAAA-12345
cgi@tonga> (/home/cgi) $

You have to use --posix to use any posix regex

To extract those fields, you could try:

cgi@tonga> (/home/cgi) $ awk -F"[a-z ]" '{print $NF}' test.txt
ABCDE-12345
BCDA-12345
ABCD-12345
AAAA-12345

This is the final shot, this should will extract the patterns

cgi@tonga> (/home/cgi) $ cat test.txt
ABCDE-12345
abc BCDA-12345
defe erjhj ABCD-12345
dfjfh sdhjh jkfgh dfj aaaAAAA-12345
sijefisfej AAAA-12345sefsfsef
sijefisfejAAAA-12345sefsfsef


cgi@tonga> (/home/cgi) $ gawk --posix -F"[a-z ]" '{for(i=1;i<=NF;i++){if($i~/^[A-Z][A-Z]{3,4}-[0-9]{5}/){print $i} }}' test.txt
ABCDE-12345
BCDA-12345
ABCD-12345
AAAA-12345
AAAA-12345
AAAA-12345
cgi@tonga> (/home/cgi) $

hii
thanks all for ur reply but the problem is that
gawk does not work on my machine...:frowning:

i think it is a simple problem because suppose my string is

 
ABCDE-12345
sdfsdjfhsdABCD-12345
fjdhgfdABC-12345
ABCE-12345

so from this above file it should only return

 
 
ABCD-12345
ABCE-12345

It also works with awk, same command.

Here you go

cgi@tonga> (/home/cgi) $ cat test.txt
ABCDE-12345
sdfsdjfhsdABCD-12345
fjdhgfdABC-12345
ABCE-12345
cgi@tonga> (/home/cgi) $ awk --posix -F"[a-z ]" '{for(i=1;i<=NF;i++){if($i~/^[A-Z][A-Z]{3}-[0-9]{5}/){print $i} }}' test.txt
ABCD-12345
ABCE-12345

@above

many many thanks for ur reply
but here comes another problem :):frowning:

the problem is u have defined field separator as [a-z] but it could be any like the pattern can be like that also

${tag}ABCD-12345

so will this code work in that scenario.too

waiting for ur reply..

You need to give us enough information, otherwise we try to exploit what ever we see in your sample. Post your input that reassemble the actual one.

hiii
sry for not providing the actual input

now here it is

my input file contains the data as

 
CLF-123454564564564.
this bug no is ABC-1234599
this bug no for bug 222 defiend in SVN is  ABC-12345
this bug no is ABCDE-12345
this bug no is ABCD-12345
and SV bug is ${111}ABCD-12345

so i need to find the error code in this file for the pattern below

 
[A-Z][A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9][0-9]