DomainsFeatured
DomainsFeatured

Reputation: 1506

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.

Upvotes: 0

Views: 255

Answers (2)

Cyrus
Cyrus

Reputation: 88774

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

Upvotes: 2

janos
janos

Reputation: 124724

This is very easy to do using awk:

while read LINE; do awk -v pattern="$LINE" '$0 ~ pattern { print pattern "," $0 }' urls.txt; done < domains.txt

Or using sed:

while read LINE; do sed -ne "s?$LINE.*?$LINE,&?p" urls.txt; done < domains.txt

To answer your follow-up question, to limit the result to the first match using awk:

while read LINE; do awk -v pattern="$LINE" '$0 ~ pattern { print pattern "," $0; exit }' urls.txt; done < domains.txt

Upvotes: 1

Related Questions