0
$ echo '<a href="mailto:NA?Subject=AB42525216 - FOOBAR bla bla - bla">NA</a>'
<a href="mailto:NA?Subject=AB42525216 - FOOBAR bla bla - bla">NA</a>
$ echo '<a href="mailto:NA?Subject=AB42525216 - FOOBAR bla bla - bla">NA</a>' | sed 's/SOMEMAGIC/NA/g'
NA

My question: how can I remove the string in the "echo" with SOMEMAGIC? The delimiter could be > I think

  • What part of string do you want to remove? – cuonglm May 30 '14 at 8:36
  • Usually it's better to use a parser to process HTML. – choroba May 30 '14 at 8:56
  • sed 's/.*>\(.*\)<.*/\1/' would work on that particular input. But so would cut -d \> -f2- | cut -f\< -f1 – Stéphane Chazelas May 30 '14 at 9:12
  • @evachristine: Do you mean replace the whole line by NA? – cuonglm May 30 '14 at 9:16
0

I think you are trying to extract the value that are inside <a href>xx</a> tag. If yes then your command should be like this,

GNU sed:

sed -r 's/^<a [^>]*>([^<]*)<.*$/\1/g' file

Traditional sed:

sed 's/^<a [^>]*>\([^<]*\)<.*$/\1/g' file

Example:

$ echo '<a href="mailto:NA?Subject=AB42525216 - FOOBAR bla bla - bla">NA</a>' | sed -r 's/^<a [^>]*>([^<]*)<.*$/\1/g'
NA

$ echo '<a href="mailto:NA?Subject=AB42525216 - FOOBAR bla bla - bla">NA</a>' | sed 's/^<a [^>]*>\([^<]*\)<.*$/\1/g'
NA

$ echo '<a href="mailto:NA?Subject=AB42525216 - FOOBAR bla bla - bla">fooooooooooooooobaaaaaaaaaar</a>' | sed 's/^<a [^>]*>\([^<]*\)<.*$/\1/g'
fooooooooooooooobaaaaaaaaaar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.