簡體   English   中英

如何用變量替換 awk 命令的管道輸出

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

我正在嘗試獲取一列並通過 echo 命令將其通過管道傳輸。 如果可能,我希望將其排成一行或盡可能高效地執行此操作。 在研究時,我發現我必須使用單引號來擴展變量並轉義雙引號。

這是我正在嘗試的:

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

但是,我一直得到的行數比單行的輸出多。 如果您知道如何捕獲字段 4 中的每一行,那將非常有幫助。

文件1.txt:

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

期望的輸出:

<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>

或者稍微簡短但不那么健壯:

$ 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