DomainsFeatured
DomainsFeatured

Reputation: 1506

How To Substitute Piped Output of Awk Command With Variable

I'm trying to take a column and pipe it through an echo command. If possible, I would like to keep it in one line or do this as efficiently as possible. While researching, I found that I have to use single quotes to expand the variable and to escape the double quotes.

Here's what I was trying:

awk -F ',' '{print $2}' file1.txt | while read line; do echo "<href=\"'${i}'\">'${i}'</a>"; done

But, I keep getting the number of lines than the single line's output. If you know how to caputure each line in field 4, that would be so helpful.

File1.txt:

Hello,http://example1.com
Hello,http://example2.com
Hello,http://example3.com

Desired output:

<href="http://example1.com">http://example1.com</a>
<href="http://example2.com">http://example2.com</a>
<href="http://example3.com">http://example3.com</a>

Upvotes: 2

Views: 479

Answers (1)

Ed Morton
Ed Morton

Reputation: 204731

$ awk -F, '{printf "<href=\"%s\">%s</a>\n", $2, $2}' file
<href="http://example1.com">http://example1.com</a>
<href="http://example2.com">http://example2.com</a>
<href="http://example3.com">http://example3.com</a>

Or slightly briefer but less robustly:

$ sed 's/.*,\(.*\)/<href="\1">\1<\/a>/' file
<href="http://example1.com">http://example1.com</a>
<href="http://example2.com">http://example2.com</a>
<href="http://example3.com">http://example3.com</a>

Upvotes: 1

Related Questions