Need a word which just comes next to after grep of a specific word

Hi,

Below is an example :

ST1 PREF: int1 AVAIL: int2
ST2 PREF :int1 AVAIL: int2

I need int1 to come in preferred variable while programming and int2 in available variable

Please help me doing so

Best regards,
Vishal

Like this ?

$ pref=$(awk '/PREF/{print $3;exit}' file)

$ avail=$(awk '/AVAIL/{print $NF;exit}' file)

What have you tried so far?

The grep will get you the whole line that matches, so then you just read the part you need from that line.

Robin

@vishal please be specific about your requirement

Grep you can try something like this

$ pref=$(grep -Po -m1 '(?<=PREF:).*(?=AVAIL:)' file)
$ echo $pref
int1 

$ avail=$(grep -Po -m1 '(?<=AVAIL:).*' file)
$ echo $avail
int2
1 Like

Thank you Akshay for the great commands. Could you please explain the same please, will be grateful to you.

Thanks,
R. Singh

grep -Po -m1 '(?<=PREF:).*(?=AVAIL:)'

P ---> Interprets as regular perl expression

o ---> Printout only what is matched

m 1 --> Max count 1, after match stop reading file

( --> start grouping

?<=PRES: --> positive look behind, but don't match PRES:

) --> end grouping

.* --> anything after positive look behind to negative look behind will be part of match

( --> start grouping

?=AVAIL: --> negative look behind

) --> end grouping

Hope its fine.

1 Like

Thanks Akshay!

However,this is not working in aix machine

 pref=$(grep -Po -m1 '(?<=PREF:).*(?=AVAIL:)' /tmp/test.out)
grep: illegal option -- P
grep: illegal option -- o
grep: illegal option -- m
grep: illegal option -- 1

Please help

Best regards,
Vishal

@Vishal :

I have no idea about AIX, I tested it on Centos 6.5, alternate solution using awk you can use if grep doesn't work, or else you may wait until someone else provide you a grep solution for AIX.

Hello,

We can try the following codes to get the same.

echo "ST1 PREF: int1 AVAIL: int2" |  sed 's/\(.*: \)\(.*\)\( AVAIL:.*\)/\2/1;1q' 
int1
echo "ST2 PREF :int1 AVAIL: int2" | sed 's/\(.*AVAIL: \)\(.*\)/\2/g;1q' 
int2

this may help you.

Thanks,
R. Singh

1 Like

..... and so you learn the valuable lesson that you need to specify the OS flavour and version to help us to help you.

You might have to go with something simpler like:-

grep expression | while read col1 col2 col3 col4 col5 .....
do
   # some action for values returned
done

If you need to remove the : then have a look at the tr command.

I hope that this helps,
Robin

Great! This is working absolutely fine

Please help me also in understanding this code.

Best regards,
Vishal

---------- Post updated 12-17-13 at 04:14 AM ---------- Previous update was 12-16-13 at 12:01 PM ----------

Hi Ravinder,

The sed command worked absolutely fine.
Can you please help me with a sed command for below code also in order to find Preferred and available instances values which are in the last

srvctl config service -s boms_cmdbut_01.world -d cmdbut
Service name: boms_cmdbut_01.world
Service is enabled
Server pool: cmdbut_boms_cmdbut_01.world
Cardinality: 1
Disconnect: false
Service role: PRIMARY
Management policy: AUTOMATIC
DTP transaction: false
AQ HA notifications: false
Failover type: NONE
Failover method: NONE
TAF failover retries: 0
TAF failover delay: 0
Connection Load Balancing Goal: LONG
Runtime Load Balancing Goal: NONE
TAF policy specification: NONE
Preferred instances: cmdbut_01
Available instances: cmdbut_02

Best regards,
Vishal