Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surrounding whitespace-separated URLs with quotes using sed

Problem

I was trying to get sed command to do the same thing I could do with Python regex flavour, but I encountered some problems

Python regex example: (tested it on regex101 and it was working fine)

find: (https.*?)

replace: "\1"

Unsuccessful code:

sed 's/\(https.*?\)[:space:]/\"\1\"/g' .\elenco.txt

elenco.txt file:

https://www.youtube.com/watch?app=desktop&v=Ot34P0yyQqI&t=984s https://www.youtube.com/watch?v=vviniZjvDQs  https://www.youtube.com/watch?v=Ih7qgkyo_oo  https://www.youtube.com/watch?v=X6UEDpwI3HI  https://www.youtube.com/watch?v=nShgaRMNlLw  https://www.youtube.com/watch?v=nd_jN-C_Juw  https://www.youtube.com/watch?v=aOtqox2uB3Y

Expected output:

"https://www.youtube.com/watch?app=desktop&v=Ot34P0yyQqI&t=984s" "https://www.youtube.com/watch?v=vviniZjvDQs"  "https://www.youtube.com/watch?v=Ih7qgkyo_oo"  "https://www.youtube.com/watch?v=X6UEDpwI3HI"  "https://www.youtube.com/watch?v=nShgaRMNlLw"  "https://www.youtube.com/watch?v=nd_jN-C_Juw"  "https://www.youtube.com/watch?v=aOtqox2uB3Y"

Actual output:

"https://www.youtube.com/watch?"pp=desktop&v=Ot34P0yyQqI&t=984s https://www.youtube.com/watch?v=vviniZjvDQs  https://www.youtube.com/watch?v=Ih7qgkyo_oo  https://www.youtube.com/watch?v=X6UEDpwI3HI  https://www.youtube.com/watch?v=nShgaRMNlLw  https://www.youtube.com/watch?v=nd_jN-C_Juw  https://www.youtube.com/watch?v=aOtqox2uB3Y

Info

OS

Name: Microsoft Windows 11 Home Version: 10.0.26100 N/D build 26100

installed sed through winget install bmatzelle.Gow

I've always avoided using POSIX regex etc, as I found it unnecessarily complicated / limited compared to using perl/python etc. and the regex flavour available there.

Any other options than to install Perl/Python? 200MB for StrawberryPerl (Perl on Windows) seems to be quite overkill and useless bloat just to have access to perl flavour regex, and sed unlike perl doen't support 'easy' regex...

https://askubuntu.com/questions/1050693/sed-with-pcre-like-grep-p

like image 649
Signor Pizza Avatar asked Dec 03 '25 22:12

Signor Pizza


2 Answers

If you can use sed's -E (--regexp-extended), it'll match more how you expect

sed -E 's/(http[^ ]+)/"\1"/g' elenco.txt
like image 127
ti7 Avatar answered Dec 05 '25 10:12

ti7


The non-white-space pattern \S (uppercase \S opposite of \s) documented for sed here was chosen because often we need to match spaces or non-spaces in regexp:

sed 's/\(https\S*\)/"\1"/g' elenco.txt

same as

sed -E 's/(https\S+)/"\1"/g' elenco.txt

like image 26
Marco Munari Avatar answered Dec 05 '25 11:12

Marco Munari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!