1

I am trying to get to a sed command to format (HTML) the first two letters of every word from a plaintext input. I would like to input a plain text and mark the first two letters bold with the <b> tag to form an html file representing the same text but with formatting to look something like this: https://www.reddit.com/media?url=https%3A%2F%2Fi.redd.it%2Fzwp75sv6dgqb1.png

Unfortunately, I am not able to get the first two letters but it works with only the first letter. So I got this:

sed -e 's/\(\b\w{1,2}\)/<b>\1<\/b>/g' test.txt > test.html

but it does not work as intended

input: this is a test
output: this is a test
intended: <b>th</b>is <b>is</b> <b>a</b> <b>te</b>st

IDK why, but this command does work to mark the first letter only:

sed -e 's/\(\b\w\)/<b>\1<\/b>/g' test.txt > test.html

why not the other one where i mark the \w(ord character) for {1,(or)2} time occasion even though regex101 shows me I should be right (https://regex101.com/r/hBb6Yt/4)?

1 Answer 1

2

Like (capturegroup), {min,max} is an EXTENDED regexp feature and must be backslashed in sed unless your sed has, and you use, an option for extended regexps (GNU -E -r --regexp-extended; BSD/Mac? -E). Also you don't need the capturegroup here:

sed -e 's/\b\w\{1,2\}/<b>&<\/b>/g' in >out
sed -Ee 's/\b\w{1,2}/<b>&<\/b>/g' in >out

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .