I have a string like this
<anytag>my message</anytag>
How I can extract the message between the tags with sed or awk?
So I get only "my message"
I have a string like this
<anytag>my message</anytag>
How I can extract the message between the tags with sed or awk?
So I get only "my message"
try:
awk -F'[><]' '{print $3}' Input_file
Making field separator as '[><]' and printing 3rd field.
Using xmllint (from libxml2):
xmllint --xpath '//anytag/text()' <(echo "<anytag>my message</anytag>")
I do not want to install xml paser for a lite extract string, my xml message is not complicated
For simple strings you may use the following sed approach:
s="<anytag>my message</anytag>"
sed 's~<[^<>]*>\([^<>]*\)</[^<>]*>~\1~' <<< $s
The output:
my message
You can use the following awk
command if each line of your file is in the format you have shown.
awk -F "<[^<]+?>" '{print $2;}' <filename>
Input:
<anytag>my message</anytag>
<mytag>abc</mytag>
Output:
my message
abc