Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression - Capture and Replace Select Sequences

Tags:

regex

sed

Take the following file...

ABCD,1234,http://example.com/mpe.exthttp://example/xyz.ext
EFGH,5678,http://example.com/wer.exthttp://example/ljn.ext

Note that "ext" is a constant file extension throughout the file.

I am looking for an expression to turn that file into something like this...

ABCD,1234,http://example.com/mpe.ext
ABCD,1234,http://example/xyz.ext
EFGH,5678,http://example.com/wer.ext
EFGH,5678,http://example/ljn.ext

In a nutshell I need to capture everything up to the urls. Then I need to capture each URL and put them on their own line with the leading capture.

I am working with sed to do this and I cannot figure out how to make it work correctly. Any ideas?

like image 960
Chad Avatar asked Feb 27 '23 22:02

Chad


1 Answers

If the number of URLs in each line is guaranteed to be two, you can use:

sed -r "s/([A-Z0-9,]{10})(.+\.ext)(.+\.ext)/\1\2\n\1\3/" < input
like image 165
Amarghosh Avatar answered Mar 11 '23 18:03

Amarghosh