Help: Need to Print a string from a word

Hi Folks

Can you please help me to grep a word from below test file

Below is the file looks like
ABCD Test[21455]:3 ZZZZYYYZZ
ABCD TEST[324494]:38 XXXYYYZZZ

I need below output
echo $A=21455
echo $B=3

and for second line

echo $A=324494
echo $B=38

I will use While read line for each line.. i just need the logic to grep those Strings from a word

Please hlp
Thanks
Machha

Try this:

 
echo $line | sed 's/.*\[\(.*\)\]:\(.*\) .*/\1 \2/' | read a b

Getting this

sed: command garbled: s/.*\[\(.*\)\]:\(.\) ./First part=\1

---------- Post updated at 02:29 PM ---------- Previous update was at 02:28 PM ----------

Command which I'm trying to run is as below

XXXX=`echo $line | sed 's/.*\[\(.*\)\]:\(.\) ./First part=\1'`

It works well for me.Which sed you are using?

You can even try the below one in awk,

 
echo $line | awk -F"[\]:\[]" '{ print $2" "$4; }' | read a b

Nope its not working for me

Getting as below

root:375$ cat test1.txt
ABCD Test[21455]:3 ZZZZYYYZZ
root:376$ cat test1.txt | awk -F"[\]:\[]" '{ print $2" "$4; }' | read a b
root:377$ echo $a
21455]:3
root:378$ echo $b
ZZZZYYYZZ

I need

root:377$ echo $a
21455
root:378$ echo $b
3

Thanks

Hi,

Please check below

$ cat read
ABCD Test[21455]:3 ZZZZYYYZZ
ABCD TEST[324494]:38 XXXYYYZZZ

$ cat read | sed 's/\(.*\)\[\(.*\)\]:\(.\) \(.\)/\2 \3/'
21455 3
324494 38

That's because you did not close the substitute command..

XXXX=`echo $line | sed 's/.*\[\(.*\)\]:\(.*\) .*/First part=\1/'`

Thanks..All,

Got it

root:389$ cat test1.txt| sed 's/\(.*\)\[\(.*\)\]:\(.\) \(.\)/\2/'
21455
324494
root:390$ cat test1.txt| sed 's/\(.*\)\[\(.*\)\]:\(.\) \(.\)/\3/'
3
38