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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep portion of a string using awk 1

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
GB
Hi,

I would like to grep 157 from here --> <text_string>157</text_string>

I prefer using awk ... please help :)

Cheers!
 
Hi

I think you want to clear all XML-like tags, so :
Code:
awk '{gsub(/<[^<>]*>/,"");print}' /input/file
But if you want to extract the value marked by two "text_string" tags, then :
Code:
awk '{print gensub(/.*<text_string>(.*)<\/text_string>.*/,"\\1","")}' /input/file
viadisky said:
I prefer using awk
I prefer [tt]sed[/tt].
Code:
sed 's/<[^<>]*>//g' /input/file
[gray]# or[/gray]
sed 's/.*<text_string>\(.*\)<\/text_string>.*/\1/' /input/file
The second solutions will not work well if there are input lines without that markup. Specify more about the input format if needed.

Feherke.
 
Hi Feherke,

Thanks for your suggestion :)

You are absolutely right, sed is better for this type of text manipultaion. It is a lot easier to intergrate it with my script.

Cheers,
Maria
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top