S
S
Sergey c0re2022-03-06 18:31:39
bash
Sergey c0re, 2022-03-06 18:31:39

How to do multiple substitutions in sed?

How to do exactly sequential replacements in one sed so that you can apply immediately to the file using the -i switch?

There is this (combing HTML table to CSV):

sed -e '/\(html\|title\|body\|table\)/d' -e 's/ \{2,\}//g' /dev/shm/ex01.html | \
  sed ':a;N;$!ba;s#\s*</td>\s*</tr>\s*<tr>\s*<td>\s*#\n#g' | \
  sed ':b;N;$!bb;s#\s*</td>\s*<td>\s*#;#g' | \
  sed '/\(tr>\|<td\|td>\)/d'


full example here

Is there any way to do this with sed alone? -e does not work, the next one does not work ':b;N;$!bb;s#\s*</td>\s*<td>\s*#;#g', and for some reason deletion only works at the beginning of the file, it does not delete at the end. PS: don't look
at the delimiters # , they are now in a specific example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey c0re, 2022-03-06
@erge

basically figured it out...
after the commands

:a;N;$!ba
the whole text becomes one line, so regular expressions are needed a little differently, see below:
sed '/title/d
     :a;N;$!ba
     s/ \{2,\}//g
     s#\s*</td>\s*</tr>\s*<tr>\s*<td>\s*#\n#g
     s#\s*</td>\s*<td>\s*#;#g
     s/<[^>]\+>//g
     s/\n\{2,\}//g' /dev/shm/ex01.html

UPDATE
the end result is this:
### Convert HTML to CSV
sed ':a;N;$!ba
     s/<html.\+<table[^>]\+>//Ig
     s#\s*</td>\s*</tr>\s*<tr>\s*<td>\s*#\n#Ig
     s#\s*</td>\s*<td>\s*#;#Ig
     s/<[^>]\+>//g;s/\s\{2,\}//g' somreport.html

example here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question