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!

How can I do this?

Status
Not open for further replies.

hmehta

IS-IT--Management
Jun 5, 2002
27
US
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....
 
Hi:

This might be cheating. The second line fails if it's a different number from &quot;866&quot;, but it follows the spec you laid out.

I'm going to be disappointed if the bigoldbulldog doesn't submit a more efficient sed script.

Regards,

Ed


#!/bin/awk

awk '
{
#Search for &quot;today is&quot; and get rid of special characters
if(match($0,&quot;Today is&quot;) )
{
gsub(&quot;<!--&quot;, &quot;&quot;, $0)
gsub(&quot;-->&quot;, &quot;&quot;, $0)
print $0
next
}

# strip out the number
i=index($0, &quot;866&quot;)
if(i > 0)
print substr($0, i, 10)
} ' data.file
 
Okay, here's a sed version

sed '
/Today is/ s/[<!-->]//g
/866/ s/.*\(866[0-9]\{7\}\).*/\1/
' data.file

It's pretty much an exact rewrite of Ed's solution. If you want to seek out all phone numbers then just look for numeric strings that are 10 characters or perhaps, better, the telephone keyword in the tag. i.e.

/Today is/ s/[<!-->]//g
/telephone/ s/.*\(866[0-9]\{7\}\).*/\1/

/Today is/ s/[<!-->]//g
/[^0-9]*\([0-9]\{10\}\)[^0-9]*/ s//\1/

Cheers,
ND [smile]
 
Oops,

the telephone tag version would be

/Today is/ s/[<!-->]//g
/telephone/ s/.*\([0-9]\{10\}\).*/\1/

In addition, you may want those separating blank lines so insert one from the /Today is/ line as

/Today is/{
1!i
s/[<!-->]//g
}
...

Cheers,
ND [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top