Need to get word after a specific word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to get word after a specific word
# 1  
Old 01-28-2016
Need to get word after a specific word

Hi,

I need hostname from below entry.How do I write a script on this?tnsping is utility in oracle -if I do tnsping ora11gb then it would give one output and I want to extract hostname from this output which come after "HOST="


Code:
oracle@brookford:~> tnsping ora11gb|grep -i HOST

Attempting to contact (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=bovina.int.westgroup.com)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ora11gb)))


Best regards,
Vishal
# 2  
Old 01-28-2016
Hello admin_db,

Could you please try following and let me know if this helps you.
Code:
awk -F"[)(]" '{sub(/.*=/,X,$6);print $6}'  Input_file

Output will be as follows.
Code:
bovina.int.westgroup.com

EDIT: Also adding a sed solution on same.
Code:
sed 's/\(.*HOST=\)\([^)]*\)\(.*\)/\2/g'  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-28-2016 at 05:45 AM.. Reason: Added a sed solution too now for same.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-28-2016
How about
Code:
tnsping ora11gb | awk 'match ($0, /HOST=[^)]*/) {print substr ($0, RSTART+5, RLENGTH-5)}'
bovina.int.westgroup.com

Small adaption to RavinderSingh13's sed proposal:
Code:
tnsping ora11gb | sed -n 's/\(.*HOST=\)\([^)]*\)\(.*\)/\2/p'

This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-28-2016
Thanks RudiC and Ravinder ! Both script are working fine.

Now,I have task as follows :
I get standby tnsentry from primary database like I get destination from below query:

Code:
SQL> select DESTINATION from v$archive_dest where target='REMOTE';

DESTINATION
------------------------------
ora11gb

Then I would extract hostname from tnsping and will transfer passwordfile to that host from primary.

Best regards,
Vishal
# 5  
Old 01-29-2016
Please rephrase.
# 6  
Old 02-01-2016
Please allow me to rephrase here .

We have a task of password change activity for databases and servers every quarter.
Now, in case I need to change db sys user password at primary I have to transfer password file to the standby database server.For this I should know after logging to primary that what is the standby site of this primary where I need to scp the file.

Now I am on primary site and my target is to copy password file to standby via script .First I will look at below query:
Code:
SQL> select DESTINATION from v$archive_dest where target='REMOTE';

DESTINATION
------------------------------
ora11gb

Now,I know tnsentry of standby .I will grep hostname from tnsentry and will scp password file to standby.So, this all I am planning to do in a script.

Code:
1. login to server ( will login to server from a group of servers via script)
2.set the environment of database one by one in case of multiple databases
3.login to database 
4.change sys password
5.Look if the primary has a standby and grep hostname from tnsentry of standby
6. scp password file to standby


Best regards,
Vishal

---------- Post updated at 06:30 AM ---------- Previous update was at 06:00 AM ----------

Hi,

For a start I have written below code but giving error and one more query what's difference if I write
Code:
x= echo $i|cut -f1 -d':'

or
Code:
x=echo $i|cut -f1 -d':'

(removed space after "=" between x and echo)

Code:
oracle@brookford:~> cat test_passwordcopy.sh
for i in `cat /etc/oratab|grep -v '^#'`
do
x= echo $i|cut -f1 -d':'
export ORACLE_SID=$x
echo $x
y= echo $i|cut -f2 -d':'
export ORACLE_HOME=$y
echo $y;
sqlplus / as sysdba<<EOF
select DESTINATION  into $z from v\$archive_dest where target='REMOTE';
EOF
echo $z;
done
oracle@brookford:~> ./test_passwordcopy.sh
./test_passwordcopy.sh: line 13: warning: here-document at line 9 delimited by end-of-file (wanted `EOF')
./test_passwordcopy.sh: line 14: syntax error: unexpected end of file
oracle@brookford:~>


Best regards,
Vishal
# 7  
Old 02-01-2016
Hi,
Not tested your complete code,on paper I can see below are wrong.

Code:
x= echo $i|cut -f1 -d':'
y= echo $i|cut -f2 -d':'

instead it should be

Code:
x=`echo $i|cut -f1 -d':'`
y=`echo $i|cut -f2 -d':'`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

3. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

4. Shell Programming and Scripting

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 (10 Replies)
Discussion started by: Vishal_dba
10 Replies

5. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

6. Shell Programming and Scripting

Fetch entries in front of specific word till next word

Hi all I have following file which I have to edit for research purpose file:///tmp/moz-screenshot.png body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: &quot;Liberation Sans&quot;; font-size: x-small; } Drug: KRP-104 QD Drug: Placebo Drug: Metformin|Drug:... (15 Replies)
Discussion started by: Priyanka Chopra
15 Replies

7. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

8. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

9. Shell Programming and Scripting

Grep out specific word and only that word

ok, so this is proving to be kind of difficult even though it should not be. say for instance I want to grep out ONLY the word fkafal from the below output, how do I do it? echo ajfjf fjfjf iafjga fkafal foeref afoafahfia | grep -w "fkafal" If i run the above command, i get back all the... (4 Replies)
Discussion started by: SkySmart
4 Replies

10. Shell Programming and Scripting

How to replace a specific word in specific column?

Hi My orginal file is like (100s of lines) id host ip location remarks 1 host1 ip1 - xxx 2 host2 ip2 - xxx 3 host3 ip3 - xxx -- -- 9 host9 ip9 - xxx I have a ref file like host1 location1 host2 location2 host3 location3 -- --... (6 Replies)
Discussion started by: ./hari.sh
6 Replies
Login or Register to Ask a Question