hello experts,
I want to get the value between 2 patterns.
ex. get hello in <line>hello</line>
Any suggestions?
any sed, grek, awk commands?
hello experts,
I want to get the value between 2 patterns.
ex. get hello in <line>hello</line>
Any suggestions?
any sed, grek, awk commands?
If the patterns are always on a single line, awk or sed should work fine. If this is a more complex file, look in the Perl FAQ (section 6 IIRC) for some canned solutions.
This is a very frequently recurring question in these forums; try searching some recent threads.
sed -n 's/.*>\([^<>]*\)<.*/\1/p' file
echo "<line>hello</line>" | sed -e 's?\(</.*>\)??' -e 's?\(<.*>\)??'
Another approach:
sed 's/.*>\(.*\)<.*/\1/' file
Regards
echo '<line>hello</line>' | awk -F"\>|\<" '{print $3}'
Except that will also get anything outside of the separators. And you don't need the parens if you don't use them for backreferences. Anyway, it would be useful to have more precise requirements. Is there text outside of the separators? Do they span multiple lines? Are there other <tags> which are not to be used as separators? Can there be multiple occurrences of the tag pairs on one line?
sed -n 's%.*<line>%%; T; s%</line>.*%%p' file
Edit: I was referring to shamrock's solution. Wow, this is a popular thread.
yes, there are more text outside the separators. I'm trying to change the command people provide me to make it more suitable for my case.
and maybe my sed is too old, I get unrecognized command with
sed -n 's%.<line>%%; T; s%</line>.%%p'
Thanks you
file:
<b>asdfasdfasdf</b>
<line>hello</line>
<a>asdfasdfasdf</c>
output:
hello
You're correct era...I focussed on the text between the tags and if there is some outside then sed will get that too which is a mistake.
perl -nle 's%.*<line>(.*?)</line>.*%$1% and print' file
Should work provided you don't have <line>foo</line><line>bar</line> lines or stuff straddlling a line break.
And the first one I posted might still work with some minor modifications; but if your sed is too sucky, it's not much fun trying to second-guess what it might accept.
sed 's%.*<line>\([^<>]*\)</line>.*%\1%p' file
Some seds don't want backslashes before the parentheses; try without them if this doesn't work.
Another one with sed:
sed -n '/<line>/s/<[^<]*>//gp' file
Regards
thank you thank you