Regular Expression - Capturing and replacing selection sequences

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 the permanent file extension in the file.

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

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 collect everything up to the URL. Then I need to commit each URL and put them in my line with the lead capture.

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

+3
source share
3 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
+5
source

, ( ) . .

sed 's/\(\([^,]*,\)\{2\}\)\(.*\.ext\)\(http:.*\)/\1\3\n\1\4/' inputfile.txt

"2", .

+1

I currently don't have sed.

Will not be

sed -r 's/(....),(....),(.*\.ext)(http.*\.ext)/\1,\2,\3\n\1,\2,\4/g' 

do the trick?

Edit : removed lazy quantifier

0
source

Source: https://habr.com/ru/post/1747489/


All Articles