Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract nos from the file

Status
Not open for further replies.

hmehta

IS-IT--Management
Jun 5, 2002
27
US
Hoe can I use awk to extract the nos from the following xml
file. I want the output to look like:
staging: 8662578522
uat:86634422000

<!--staging-->
<if cond=&quot;session.telephone.dnis=='8662578522'&quot;>

<!--uat-->
<if cond=&quot;session.telephone.dnis=='86634422000'&quot;>
 
Hi hmehta,

This works for the data you gave, but you may need to scan the line if the actual data is more variable.
Code:
/<!--staging-->|<!--uat-->/ {
  a=substr($0,5,length-7)
  getline
  print a &quot;:&quot; substr($0,36,length-38)
}
Hope this helps. CaKiwi
 
Here are two sed solutions -

sed '
/^[ ]*$/d
/<!--staging-->/b chg
/<!--uat-->/{
:chg
s/[^a-z]*//g
h
N
s/[^0-9]*//g
x
G
s/\n/:/
}' < input

sed '
/^[ ]*$/d
/<!--staging-->/b chg
/<!--uat-->/{
:chg
N
s/^[^a-z]*\([a-z][a-z]*\)[^\n]*\n[^0-9]*\([0-9][0-9]*\).*/\1:\2/
' < input

The first is about 30% faster than the awk solution, which is much better (80%) than I could do in awk, and the second is twice as fast as my first sed solution.

Cheers,
ND [smile]
 
How can I do the following:
In my file I do not know where this string will come but whenever I see the following two lines in the following format I need to extract two things.
a. The complete String minus the special characters(<-->)in line 1
b. The 866 phone no in line2. Also I cannot be sure at which position this no would be.

<!--Today is 3rd May 2002-->
<if cond=&quot;session.telephone.dnis=='8663442000'&quot;>
-------
---------
-------
------

<!--Today is 4th May 2002-->
<if cond=x='8663442001'&quot;>

My final output should look like:
Today is 3rd May 2002
8663442000

Today is 4th May 2002
8663442001

How can I do this....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top