How to extract only relevant part from a sentence?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract only relevant part from a sentence?
# 1  
Old 11-26-2015
How to extract only relevant part from a sentence?

Hello All,

I have a file with details such as below. How do i extract only the
host and port ?

eg: dbs.ads.com 1521


Code:
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dbs.ads.com)(PORT=1521))(CONNECT_DATA=(SID=vug)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=captain.adsoe.com)(PORT=1521))(CONNECT_DATA=(SID=x10)))
(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 11.69.21.37)(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = o901.ads.com)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ap60.as.com)(PORT=1521))

TIA,
John

Last edited by Don Cragun; 11-26-2015 at 03:09 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 11-26-2015
Hello JohnJacobChacko,

Following may help you in same, it will work if your Input_file has data same format as per shown sample input.
Code:
awk '{gsub(/.*HOST/,X,$0);gsub(/\)\).*/,X,$0);gsub(/\)\(/," ",$0);gsub(/PORT| = |=/,X,$0);print $0}'  Input_file

Output will be as follows.
Code:
dbs.ads.com 1521
captain.adsoe.com 1521
11.69.21.37 1521
ap60.as.com 1521

EDIT: Adding a sed solution for also.
Code:
sed -e 's/.*HOST\| = \|=\|)).*//g;s/)(PORT/ /;' Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-26-2015 at 02:06 AM.. Reason: Added a sed solution also now.
# 3  
Old 11-26-2015
May this help you

Code:
[akshay@localhost tmp]$ cat file
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dbs.ads.com)(PORT=1521))(CONNECT_DATA=(SID=vug)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=captain.adsoe.com)(PORT=1521))(CONNECT_DATA=(SID=x10)))
(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 11.69.21.37)(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = o901.ads.com)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ap60.as.com)(PORT=1521))

Code:
[akshay@localhost tmp]$ awk '/HOST/{h=$2}/PORT/{p=$2}h && p{print h,p; h=p=""}' FS='='  RS=')' file
dbs.ads.com 1521
captain.adsoe.com 1521
 11.69.21.37  1521
ap60.as.com 1521

# 4  
Old 11-26-2015
When I tried RavinderSingh13's sed suggestion with the supplied sample input, I got the output:
Code:
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dbs.ads.com =1521))(CONNECT_DATA=(SID=vug)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=captain.adsoe.com =1521))(CONNECT_DATA=(SID=x10)))
(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 11.69.21.37  = 1521)))(CONNECT_DATA = (SERVICE_NAME = o901.ads.com)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ap60.as.com =1521))

His suggestion might work with GNU sed, but it doesn't work with the sed on OS X. You might want to try:
Code:
sed 's/.*HOST *= *\([.[:alnum:]]*\).*PORT *= *\([[:digit:]]*\).*/\1 \2/' file

which, with a standard sed produces the output:
Code:
dbs.ads.com 1521
captain.adsoe.com 1521
11.69.21.37 1521
ap60.as.com 1521

With a GNU sed, the following should work:
Code:
sed --posix 's/.*HOST *= *\([.[:alnum:]]*\).*PORT *= *\([[:digit:]]*\).*/\1 \2/' file

# 5  
Old 11-26-2015
An alternative to sed or awk.
Code:
cat johnjacobchacko.file

Code:
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dbs.ads.com)(PORT=1521))(CONNECT_DATA=(SID=vug)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=captain.adsoe.com)(PORT=1521))(CONNECT_DATA=(SID=x10)))
(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 11.69.21.37)(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = o901.ads.com)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ap60.as.com)(PORT=1521))
Has nothing of Possible interest.
Here is another line with nothing of interest.

Code:
perl -nle '/H\w+\W+(.+)\)\(P\w+\W+(\d+)/ and print "$1 $2";' johnjacobchacko.file

Code:
dbs.ads.com 1521
captain.adsoe.com 1521
11.69.21.37 1521
ap60.as.com 1521

# 6  
Old 11-28-2015
Code:
awk -F'[)]|[ \t]*=[ \t]*' '/HOST/{h=$2} /PORT/{print h, $2}' RS=\( file

or
Code:
awk -F'[=)]' '/HOST/{h=$2} /PORT/ {print h, $2}' RS=\( file

if you don't mind spurious spaces in the output



--
sed:
Code:
sed 's/.*HOST[^=]*=[^=]\([^)]*\).*PORT[^=]*=[^=]\([^)]*\).*/\1 \2/' file


Last edited by Scrutinizer; 11-28-2015 at 02:22 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract sentence and its details from a text file based on another file of sentences

Hi I have two text files. The first file is TEXTFILEONE.txt as given below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (7 Replies)
Discussion started by: my_Perl
7 Replies

2. UNIX for Advanced & Expert Users

Need help to extract part of the string

Hi, I have a string with name as 20140412-s1-Potopolive_promos_20140412. So I want to extract only Potopolive string. Could you please help me the command. O/p : Potopolive Thx in advance (5 Replies)
Discussion started by: lkeswar
5 Replies

3. Shell Programming and Scripting

Extract part of file

Hello All, I need to extract part of a file into a new file My file is Define schema xxxxxx Insert into table ( a ,b ,c ,d ) values ( 1, 2, 3, (15 Replies)
Discussion started by: nnani
15 Replies

4. Shell Programming and Scripting

Replacing part of the sentence using echo and sed

Hi, Iam using ksh and trying to execute the following syntax to replace one word of the sentence with a new word. But somehow sed is not able to replace the old value with new value. Please let me know where Iam going wrong. Sample Code : --> export line="VORTEX,abcdef" export... (3 Replies)
Discussion started by: ajithab
3 Replies

5. Shell Programming and Scripting

Extract a word from sentence

$SET_PARAMS='-param Run_Type_Parm=Month -param Portfolio_Parm="997" -param From_Date_Parm="2011-08-09"' Want to extract the value of "Portfolio_Parm" from $SET_PARAMS i.e in the above case "997" & assigned to new variable. The existence order of "Portfolio"Parm" can change, but the name... (2 Replies)
Discussion started by: SujeethP
2 Replies

6. Shell Programming and Scripting

extract a field from a long sentence!

Hi, I want to extract the value of LENGTH column (high-lighted in red) from a file which will have several lines as shown below: <INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME ="UNIX" NULLABLE... (4 Replies)
Discussion started by: dips_ag
4 Replies

7. Shell Programming and Scripting

extract from a long sentence

Hi, There's a long sentence from which I need to extract only the part which is at the right side of the word LENGTH i.e. 15 long sentence : <INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME... (3 Replies)
Discussion started by: dips_ag
3 Replies

8. Shell Programming and Scripting

Trim the sentence containing colon and period to extract a word in between

Hello All , i am a newbie in korn shell scripting trying to trim a sentence that is parsed into a variable . The format of the sentence has three words that are separated from other by a " : " colon and "." period . Format of the sentence looks like ... (5 Replies)
Discussion started by: venu
5 Replies

9. UNIX for Dummies Questions & Answers

Script to ask for a sentence and then count number of spaces in the sentence

Hi People, I need some Help to write a unix script that asks for a sentence to be typed out then with the sentence. Counts the number of spaces within the sentence and then echo's out "The Number Of Spaces In The Sentence is 4" as a example Thanks Danielle (12 Replies)
Discussion started by: charlie101208
12 Replies

10. Shell Programming and Scripting

extract the relevant data files for a quarter

CTB_KT_OllyotLvos_20081204_164352_200811.txt CTB_KT_LN_utahfwd_20081204_164352_200811.txt CTB_KT_LN_utahfwd_Summ_20081204_164352_200811.txt CTB_KT_PML_astdt_prFr_20081204_210153_200811.txt CTB_KT_PML_astdt_prOt_20081204_210153_200811.txt CTB_KT_PML_astdt_Nopr_20081204_210153_200811.txt... (7 Replies)
Discussion started by: w020637
7 Replies
Login or Register to Ask a Question