Extracting a field value from a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting a field value from a line
# 1  
Old 05-06-2009
Extracting a field value from a line

Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.12.11.255)(PORT = 1540)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = CSMFIRE03)))

I would like to extract the CSMFIRE03.

I have no clue which command to use. Could any of you in this forum reply with the solution.
# 2  
Old 05-06-2009
Hi Naresh,

Might be not efficient but will giv you the solution

Code:
 
cat tns_names.txt |tr '(' '\n' |sed 's/)/ /g' | awk '/SERVICE_NAME/ { print $3}'

Hope for some other good answers Smilie
# 3  
Old 05-06-2009
Thank you panyam for the solution.

In the same way if i want to extract the IP address from that sentence how could i do that. Do you have any solution for this.

I am lil bit confused in using sed and awk . Do you have any suggestions for these which helps me in remembering them any link for the info abt them.

Thank You.
# 4  
Old 05-06-2009
Naresh,

The solution remain almost same, only change is u need to search for the "HOST" instead of "SERVICE_NAME".

Code:
cat tns_names.txt |tr '(' '\n' |sed 's/)/ /g' | awk '/HOST/ { print $3}'

You can google and you will get so many good doc's and information related to awk and sed.

Regards
Sastry
# 5  
Old 05-06-2009
To get the SERVICE_NAME:

Code:
sed -n 's/.*SERVICE_NAME = \(.*\))))/\1/p' file

For the IP address:

Code:
sed -n 's/.*HOST = \(.*\))(.*/\1/p' file

# 6  
Old 05-06-2009
Thank You panyam and franklin..

I got it just now. I will google tr ,sed and then awk.

I executed the command in pieces and seen the output. I have understood 50 % of it.

Thank you once again. :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

2. Shell Programming and Scripting

Extracting field values from .csv

How can I select the bold fields from the following? "CLLI","SWREL","RPTDATE","RPTIME","TZ","RPTTYPE","RPTPD","IVALDATE","IVALSTART","IVALEND","NUMENTIDS" "tklc9010801","EAGLE5 45.0.0-64.70.1","2013-08-07","02:01:50","MST ","COMPONENT MEASUREMENTS ON... (4 Replies)
Discussion started by: leghorn
4 Replies

3. Shell Programming and Scripting

awk - extracting a field

Hi all, I have automated a process of doing a sanity check for the DDL created by other DBA's. But i am stuck at a point. while reading a file in the awk, PI of the table is defined as )PRIMARY INDEX test_abc(ACCT_ID) PARTITION BY RANGE_N(AUTHZN_MTCH_IND BETWEEN DATE '2011-10-01' AND... (1 Reply)
Discussion started by: ysvsr1
1 Replies

4. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

6. Shell Programming and Scripting

Extracting duplicates from a desired field

Hello, I have a file of group names and GID's (/etc/group) and I want to find the duplicate group names and put them in a file. So there are 2 fields, i.e.: audit 10 avahi 70 avahi-autoipd 103 bellrpi 605 bin 1 bin 2 bord 512 busobj 161 bwadm 230 cali81 202 card 323 cardiff 901 cbm... (2 Replies)
Discussion started by: mgb
2 Replies

7. Shell Programming and Scripting

Need help with extracting field

hi there, i'm new to shell scripting as i usually just use our solaris server for db administration and to get some basic system information. however i'm stuck with something that requires further scripting knowledge. I need to extract the Name, Desc and Status from pkginfo-l. I need them... (10 Replies)
Discussion started by: sbavanis
10 Replies

8. Shell Programming and Scripting

Extracting field from array

I have a problem with the following lines of code: SaveIFS=$IFS IFS="/" declare -a Array=($1)#take unix path, split by '/' website=${Array} #stores website name The confusing part is that it works on one machine, but not on the other. If the code is... (2 Replies)
Discussion started by: montana24
2 Replies

9. Programming

Extracting Field values for XML file

i have an input file of XML type with data like <nx-charging:additional-parameter name="NX_INTERNATIONALIZED_CLID" value="919427960829"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-charging:process> i want to extract data such that i get the output... (3 Replies)
Discussion started by: junaid.nehvi
3 Replies

10. Shell Programming and Scripting

extracting a field from directory path ??????

Hi I'll be getting a directory path as the input to the script. E.g. 1 abc/fsg/sdfhgsa/fasgfsd/adfghad/XXX/fhsad e.g. 2 sadfg/sadgjhgds/sd/dtuc/cghcx/dtyue/dfghsdd/XXX/qytq This input will be stored in a variable. My query is how to extract the field in a variable VAR which occurs... (15 Replies)
Discussion started by: skyineyes
15 Replies
Login or Register to Ask a Question