Skip to content

Instantly share code, notes, and snippets.

@daveydee33
Last active March 29, 2022 04:20
Show Gist options
  • Save daveydee33/eb007dad5f1745d7ccc5c06ccd0af085 to your computer and use it in GitHub Desktop.
Save daveydee33/eb007dad5f1745d7ccc5c06ccd0af085 to your computer and use it in GitHub Desktop.
Extract an HTML tag value using sed with regular expression

Extract the content from inside an HTML tag with sed and a regex

example <title>this is it</title> if we want to extract just this is it.

echo '<title>this is it</title>' | sed -nE 's/<title>(.*)<\/title>/\1/p'

Regex Find and select HTML attributes - such as a "class" name, even if it spans multiple lines.

(class=".*?")

Add a whitespace character to pad all content within brackets

I had to use this to do a global find/replace all within many files to reformat things like:

{{this}}

to

{{ this }}

Using gnu-sed instead of OS X sed because of compatability issues using -i with Mac version of sed. (brew install gnu-sed)

find . -type f -name '*.njk' | while read file; 
do 
  gsed -i 's/{{\(\S\)/{{ \1/g' $file; 
  gsed -i 's/\(\S\)}}/\1 }}/g' $file; 
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment