Combining sed commands in bash

Viewed 61

I am aiming to try and combine the two following sed commands to print out one output. The first command is used to strip the HTML file of its HTML tags and the second is to specify I only want lines 11 through to 16 of the file.

sed -e 's/<[^>]*.//g' file.html

sed -n '11,16p' file.html

I have been playing around with this for a while now and can only ever seem to get the output of lines 11-16 with the HTML tags, or all lines without the HTML, when I am aiming to display the output of lines 11-16 without any HTML tags. Any help would be greatly appreciated, thanks!

2 Answers

The naive way would be to use a pipe:

sed 's/<[^>]*.//g' file.htm | sed -n '11,16p'

You may also combine the address and the pattern:

sed -n '11,16 s/<[^>]*.//pg' file.html

Here,

  • -n will suppress the default line output
  • 11,16 - will set the address, Lines 11 through 16
  • s/<[^>]*.// - will look for <, then zero or more chars other than > and then any one char (did you mean a >?)
  • p - print the result of the substitution
  • g - all occurrences on the line

An online demo (shortened version, Lines 2-4):

#!/bin/bash
s="<111111>aaa<111111>
<22222>bbb<111111>
<33333>ccc<111111>
<44444>ddd<111111>
<55555>eee<111111>"
sed -n '2,4 s/<[^>]*.//pg' <<< "$s"

Output:

bbb
ccc
ddd

If GNU-compatible,

sed -n '11,16{ s/<[^>]*.//g; p; }; 17q;' file.html

The range will take a block, allowing both commands to be done sequentially to each line. The 17q; just keeps it from wasting time on lines you already know you don't need.

Related