1

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!!

0
0

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
3

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
0

A perl solution:

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

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

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.