Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

INPUT:

$ cat a.txt
FOO<td align="right" style='mso-number-format:"\[$-409\]m\/d\/yy\\ h\:mm\\ AM\/PM\;\@";' x:str>BAR

OUTPUT:

$ sed 's/SOMEMAGIC//g' a.txt
FOOBAR

My question: How can I remove that horrible "< td align......" part? This drives me nuts!!

share|improve this question

3 Answers 3

up vote 0 down vote accepted

You could also use this,

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

Explanation:

^\([^<]*\)< - Fetches any charcter not of < zero or more times from the starting position upto < and finally stores the fetched characters into a group.

.*> - Matches any character zero or more times until it finds >.

\(.*\)$ - Once the sed finds > character, it starts to store all the characters which are next to > upto the last into another group(stores characters inbetween > and $).

Finally sed prints only the stored groups(\1,\2) through back-reference.

Example:

$ cat file.txt
FOO<td align="right" style='mso-number-format:"\[$-409\]m\/d\/yy\\ h\:mm\\ AM\/PM\;\@";' x:str>BAR
$ sed 's/^\([^<]*\)<.*>\(.*\)$/\1\2/g' file.txt
FOOBAR
share|improve this answer

Well, that certainly is easy:

sed -i 's/<.*>//' file

There isn't too much to explain here:

  • the < is the start of the part we want to match
  • .* means any character (.) and any amount (*). This is a cannon to kill a mosquito, but should work for the non-esoteric examples
  • > end of the match.

Here is live:

➜  ~  cat test 
FOO<td align="right" style='mso-number-format:"\[$-409\]m\/d\/yy\\ h\:mm\\ AM\/PM\;\@";' x:str>BAR
➜  ~  sed 's/<.*>//' test
FOOBAR
share|improve this answer

A perl solution:

$ perl -F'<.*>' -anle 'print @F' file
FOOBAR

Here we use regex <.*> as delimiter to split the line instead of removing it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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