Use sed to Replace Anchor Tags

Convert links to markdown

Refer: https://digitalsanctum.com/2020/09/10/how-to-replace-html-links-with-markdown-links/

cat anchor-tag.html | sed -r 's/<a[^>]*href="([^"]*)[^>]*>([^<]*)[^>]*>/[\2](\1)/g'

This:

Refer to this: <a href="https://mruckman.com">link</a>

Changes to This:

Refer to this: [link](https://mruckman.com)

Use this command to convert ALL href tags into underline tags, this includes mailto links

cat anchor-tag.html | sed -r 's/<a[^>]*href="([^"]*)[^>]*>([^<]*)[^>]*>/\<u\>\2\<\/u\>/g'

This:

Refer to this: <a href="https://mruckman.com">link</a>

Changes to This:

Refer to this: <u>link</u>

Extract part of log file using less and sed

Sometimes when using less to view a log file, seem to need a little extra than just a screen full and the copy command just does not work well.  You can use the following two commands in combination to grab line numbers and then extract just those lines.

# Use this to get the line numbers
$ less -N hal-guest-server.log
# Use sed to extract that range to your home directory, notice the “little” p at the end of second line number
$ sed -n -e 32865,32966p hal-guest-server.log > ~/log-excerpt.txt

Simple replace with sed command

Here's an example to scan/replace recursively:

grep -lr -e ' ' * | xargs sed -i 's/ //g'

Another sample:

echo "/tmp/testme.txt" | xargs sed -i 's/XXX/YYY/g'

Another sample, the dash i parameter makes it replace inline

sed -i 's|/bin/zip|/usr/bin/zip|g' testme.txt

Using sed with an input file:

sed -f sed.txt input.txt

input.txt

1 two three 1
four 5 six

sed.txt

s/1/one/g
s/5/five/

With the following sed commands

  • delete lines with [Control]
  • replace ? with a space
  • delete any lines that contain ONLY white space
  • delete any trailing white space
  • replace Word dash with a simple -
  • replace > < with ><
  • G tells sed to double space the output file

sed.txt

/\[Control\]/d
s/?/ /g
/^\s*$/d
s/:space:*$//
s/–/-/g
s/> </></g
G