How to extract text from a line in file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to extract text from a line in file
# 1  
Old 12-21-2008
How to extract text from a line in file

I have a file abc.txt as below :

<dbport oa_var="s_dbport" oa_type="EXT_PORT" base="1521" step="1" range="-1" label="Database Port">1616</dbport>
<rpc_port oa_var="s_rpcport" oa_type="PORT" base="1626" step="1" range="-1" label="RPC Port">1721</rpc_port>
<web_ssl_port oa_var="s_webssl_port" oa_type="PORT" base="4443" step="1" range="-1" label="Web SSL Port">4538</web_ssl_port>
<ons_localport oa_var="s_ons_localport" oa_type="PORT" base="6100" step="1" range="-1" label="ONS Local Port">6195</ons_localport>
<ons_remoteport oa_var="s_ons_remoteport" oa_type="PORT" base="6200" step="1" range="-1" label="ONS Remote Port">6295</ons_remoteport>
<ons_requestport oa_var="s_ons_requestport" oa_type="PORT" base="6500" step="1" range="-1" label="ONS Request Port">6595</ons_requestport>
<web_port oa_var="s_webport" oa_type="PORT" base="8000" step="1" range="-1" label="Web Listener Port">8095</web_port>
<activewebport oa_var="s_active_webport" oa_type="DUP_PORT" base="8000" step="1" range="-1" label="Active Web Port">8095</activewebport>
<forms_port oa_var="s_formsport" oa_type="PORT" base="9000" step="1" range="-1" label="Forms Port">9095</forms_port>
<met_data_port oa_var="s_metdataport" oa_type="PORT" base="9100" step="1" range="-1" label="Metrics Server Data Port">9195</met_data_port>
<met_req_port oa_var="s_metreqport" oa_type="PORT" base="9200" step="1" range="-1" label="Metrics Server Request Port">9295</met_req_port>

Output of cat abc.txt | grep s_dbport command is

<dbport oa_var="s_dbport" oa_type="EXT_PORT" base="1521" step="1" range="-1" label="Database Port">1616</dbport>

How can I extract base="1521" from above output. Any help would be greatly appreciated.
# 2  
Old 12-21-2008
Try something like this:

Code:
awk -F"\"" '/s_dbport/{print $6}'

Regards
# 3  
Old 12-21-2008
using Perl:
Code:
perl -nl -e 'if(/s_dbport/) { if(/base="(\d+)"/) { print $1; } }' abc.txt

# 4  
Old 12-21-2008
Thanks for reply Franklin52 and Yogesh Sawant. I am not able to get whole command working since I am not able to pass value to suggested command through variable. Below command does not work.

$xmlparamdynamic=s_dbport
$base_value=`cat abc.txt | awk -F"\"" '/$xmlparamdynamic/{print $6}'`

Any helps is appreciated.
# 5  
Old 12-22-2008
try this one
cat abc.txt | grep s_dbport | awk -F" " '{ print $4 }'
# 6  
Old 12-22-2008
Quote:
Originally Posted by sai21
try this one
cat abc.txt | grep s_dbport | awk -F" " '{ print $4 }'
that's another case of useless use of cat. this would suffice:
Code:
grep s_dbport abc.txt | awk -F" " '{ print $4 }'

the awk solution provided by franklin52 even eliminates use of grep
# 7  
Old 12-22-2008
Quote:
Originally Posted by findprakash
Thanks for reply Franklin52 and Yogesh Sawant. I am not able to get whole command working since I am not able to pass value to suggested command through variable. Below command does not work.

$xmlparamdynamic=s_dbport
$base_value=`cat abc.txt | awk -F"\"" '/$xmlparamdynamic/{print $6}'`

Any helps is appreciated.
With a variable the command should looks like:
Code:
$xmlparamdynamic=s_dbport
$base_value=`awk -F"\"" '/'$xmlparamdynamic'/{print $6}' abc.txt`

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to extract digit in line of text and create link

I am trying to extract the number in bold (leading zero removed) after Medexome_xx_numbertoextractin file and create an output using that extracted number. In the output the on thing that will change is the number the other test is static and will be the same each time. Thank you :). file ... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

3. Shell Programming and Scripting

How to extract text from STRING to end of line?

Hi I have a very large data file with several hundred columns and millions of lines. The important data is in the last set of columns with variable numbers of tab delimited fields in front of it on each line. Im currently trying sed to get the data out - I want anything beetween :RES and... (4 Replies)
Discussion started by: Manchesterpaul
4 Replies

4. UNIX for Dummies Questions & Answers

extract text between two words on a single line

Hi Guys, Can someone help me with a way to extract text between two words on a single line. For example if the file has below content I want to extract all text between b and f inclusive of b and f. Aparently sed does this but does it line by line and I guess it cannot read word by word. ... (11 Replies)
Discussion started by: krishnaux
11 Replies

5. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

6. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

7. Shell Programming and Scripting

Extract pattern from text line

The text line has the following formats: what.ever.bla.bla.C01G06.BLA.BLA2 what.ever.bla.bla.C11G33.BLA.BLA2 what.ever.bla.bla.01x03.BLA.BLA2 what.ever.bla.bla.03x05.BLA.BLA2 what.ever.bla.bla.Part01.BLA.BLA2 and other similar ones, I need a way to select the "what.ever.bla.bla" part out... (4 Replies)
Discussion started by: TehOne
4 Replies

8. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

9. Shell Programming and Scripting

Extract pattern from text line

Hi, the text line looks like this: "test1" " " "test2" "test3" "test4" "10" "test 10 12" "00:05:58" "filename.bin" "3.3MB" "/dir/name" "18459" what's the best way to select any of it? So I can for example get only the time or size and so on. I was trying awk -F""" '{print $N}' but... (3 Replies)
Discussion started by: TehOne
3 Replies

10. Shell Programming and Scripting

Extract pattern from text line

Gents, from these sample lines: ZUCR.MI ZUCCHI SPA RISP NC 2,5000 6 ott 0,0000 ZV.MI ZIGNAGO VETRO 3,6475 16:36 Up 0,0075 is it possible to get this: ZUCR.MI 2,5000 ZV.MI 3,6475 i.e. the first field, a separator and the first decimal number? (in Europe we... (9 Replies)
Discussion started by: vampirodolce
9 Replies
Login or Register to Ask a Question