简体   繁体   English

结合sed和awk命令

[英]Combining sed and awk commands

I am trying to add a few columns to a file with about 500 rows in them but for now lets say I was using one file with 500 lines. 我正在尝试向其中包含约500行的文件添加几列,但现在可以说我正在使用一个包含500行的文件。

I have two commands. 我有两个命令。 One sed command and one awk command 一个sed命令和一个awk命令

The sed command is used to place a string at the front of every line. sed命令用于在每行的开头放置一个字符串。 (It works perfectly) Example of the script: (效果很好)脚本示例:

sed -e "2,$s@^@https://confidential/index.pl?Action=AgentTicketZoom;TicketID=@" C:\Users\hd\Desktop\action.txt > C:\Users\hd\Desktop\test.txt

The awk command is meant to place a string at the beginning of every line, before the sed string and increment the two numbers ( example below). awk命令用于将字符串放置在sed字符串之前的每一行的开头,并递增两个数字(以下示例)。 So technically speaking the sed command will be in column 2 and the awk command will be column 1. 因此从技术上讲sed命令将在第2列中,而awk命令将在第1列中。

I would use another sed command but sed doesn't increment values as easily. 我将使用另一个sed命令,但sed不会轻易增加值。 Please help! 请帮忙!

Example of the script: 脚本示例:

awk `{       
for (i=0; i<=10; i++)
{
printf "=HYPERLINK(B%d, C%d), \n, i"
}exit1
}`

The awk code is supposed to show something like awk代码应该显示类似

=HYPERLINK(B2,C2), https://confidential/index.pl?Action=AgentTicketZoom;TicketID=

=HYPERLINK(B3,C3), https://confidential/index.pl?Action=AgentTicketZoom;TicketID=

=HYPERLINK(B4,C4), https://confidential/index.pl?Action=AgentTicketZoom;TicketID=

=HYPERLINK(B5,C5), https://confidential/index.pl?Action=AgentTicketZoom;TicketID=

=HYPERLINK(B6,C6), https://confidential/index.pl?Action=AgentTicketZoom;TicketID=

You never need sed if you're using awk, and you should never use sed for anything other than simple substitutions on a single line. 如果您使用的是awk,则永远不需要sed,并且除了单行中的简单替换以外,您永远都不要使用sed。 Just use this awk script: 只需使用以下awk脚本:

awk 'NR>1{printf "=HYPERLINK(B%d,C%d), https://confidential/index.pl?Action=AgentTicketZoom;TicketID=%s\n", NR-1, NR-1, $0}' file

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

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