Re: Matching specific word and fetch the value assigned to that (124 Views)
Reply
Respected Contributor
RamkumarA
Posts: 215
Registered: ‎06-23-2009
Message 1 of 8 (124 Views)
Accepted Solution

Matching specific word and fetch the value assigned to that

Hi Experts,

I need to match the exact words like /clocal, /clocal/dba in the below file and fetch the corresponding values present next to those words.

shcscp19:/ $ cat /tmp/test.txt
/clocal 5
/clocal/dba 10
/clocal/ora 15
/clocal/mqm/user/mqm 20


I used grep -w option but its fetching all filesystems starting with /clocal like below.

shcscp19:/ $ grep -w /clocal /tmp/test.txt
/clocal 5
/clocal/dba 10
/clocal/ora 15
/clocal/mqm/user/mqm 20


I tried ^$ option like below, but failed to fetch the value.

shcscp19:/ $ grep ^/clocal$ /tmp/test.txt
shcscp19:/ $

Please help me to match the exact word and get the corresponding value mentioned for that filesystem.

Thanks,
Ram.
Please use plain text.
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 2 of 8 (124 Views)

Re: Matching specific word and fetch the value assigned to that

try this:

# sed -n -e 's#/clocal .*#&#p' -e 's#/clocal/dba .*#&#p' /tmp/test.txt
****
Unix operates with beer.
Please use plain text.
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 3 of 8 (124 Views)

Re: Matching specific word and fetch the value assigned to that

> I used grep -w option but its fetching all filesystems starting with /clocal like below.

the main idea is to match not to "/clocal" but to "/clocal ". so this should also work:

# grep "/clocal " /tmp/test.txt

With "-w" it does not work on my test linux system, currently I don't have access to an HP-UX.
****
Unix operates with beer.
Please use plain text.
Honored Contributor
Raj D.
Posts: 2,624
Registered: ‎06-12-2004
Message 4 of 8 (124 Views)

Re: Matching specific word and fetch the value assigned to that

Ram,

>I need to match the exact words like /clocal, /clocal/dba ..

- Why to get it from the file , as these are filesystem why not to use directly from bdf command to capture a filesystem, like:
# bdf /clocal
or
# bdf /clocal/dba

Hth,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Please use plain text.
Respected Contributor
RamkumarA
Posts: 215
Registered: ‎06-23-2009
Message 5 of 8 (124 Views)

Re: Matching specific word and fetch the value assigned to that

Hi Viktor,

Thanks, I checked grepping /clocal with space as you mentioned and it seems to be working. I need to configure the same in script for multiple filesystems. I will execute and let you know how it goes.

Thanks,
Ram.
Please use plain text.
Respected Contributor
RamkumarA
Posts: 215
Registered: ‎06-23-2009
Message 6 of 8 (124 Views)

Re: Matching specific word and fetch the value assigned to that

Hi Raj,

User want to monitor only required filesystems from a file with a threshold value... Thats the reason for reading it from a file.

Thanks,
Ram.
Please use plain text.
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 7 of 8 (124 Views)

Re: Matching specific word and fetch the value assigned to that

>User want to monitor only required filesystems from a file with a threshold value... Thats the reason for reading it from a file.

in this case I would rather use two arrays, and fill these with the values fetched directly from the config file. (test.txt)

#reading in the values to an array:

i=0
while read fs treshold
do
FS[$i]=$fs
TRESH[$i]=$treshold
(( i+=1 ))
done < /tmp/test.txt
echo $i items has been loaded from the config.

this way you would end up with two arrays: FS[] containing the filesystems to be monitored, and TRESH[] for the corresponding tresholds. The relation to these two is the common index for a specific filesystem.
****
Unix operates with beer.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 8 (124 Views)

Re: Matching specific word and fetch the value assigned to that

Hi Ram:

> Please help me to match the exact word and get the corresponding value mentioned for that filesystem.

You could do:

# perl -le '$m=shift;while (<>) {m{$m\s+(\d+)} and print $1}' /clocal /tmp/test.txt
5

# perl -le '$m=shift;while (<>) {m{$m\s+(\d+)} and print $1}' /clocal/ora /tmp/text.txt
15

This matches the string you pass as the first argument in the file passed as the second argument. A match occurs if the string sequence is found, followed by one or more spaces, followed by one or more digits. When thus matched, the digit(s) are captured and printed.

Regards!

...JRF...

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation