DomainsFeatured DomainsFeatured - 1 month ago 13
Linux Question

How To Prepend Variable Pattern By Piping Grep Output To Sed?

I have the following data in two files:

domains.txt
contains:

http://example1.com


urls.txt
contains:

http://example1.com/url-example/
http://example5.com/url-example/
http://example2.com/url-example/


Using the following command (I'm using this structure because usually there is more in the files and this is just a minimal example):

cat domains.txt | while read LINE; do grep -m 1 "$LINE" urls.txt


This will give me the matching line.

http://example1.com/url-example/


However, I would like the desired output to be:

http://example1.com,http://example1.com/url-example/


I would like to add a pipe that would prepend the
"LINE"
variable before the matched output. I was thinking something with
sed
should be easy? Your input is highly appreciated.

Update:

Although this is easy with
awk
, if someone has an answer with piping the output, I would like to use that to go with the script.

Answer
while IFS= read -r line; do echo -n "$line,"; grep -m 1 "$line" urls.txt; done < domains.txt