简体   繁体   English

如何用变量替换 awk 命令的管道输出

[英]How To Substitute Piped Output of Awk Command With Variable

I'm trying to take a column and pipe it through an echo command.我正在尝试获取一列并通过 echo 命令将其通过管道传输。 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.如果您知道如何捕获字段 4 中的每一行,那将非常有帮助。

File1.txt:文件1.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>
$ 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM