Operating System - HP-UX
1752650 Members
5673 Online
108788 Solutions
New Discussion юеВ

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

 
SOLVED
Go to solution
RamkumarA
Respected Contributor

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.
7 REPLIES 7
Viktor Balogh
Honored Contributor

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.
Viktor Balogh
Honored Contributor
Solution

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.
Raj D.
Honored Contributor

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 . "
RamkumarA
Respected Contributor

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.
RamkumarA
Respected Contributor

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.
Viktor Balogh
Honored Contributor

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.
James R. Ferguson
Acclaimed Contributor

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...