简体   繁体   中英

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.

I have two commands. One sed command and one awk command

The sed command is used to place a string at the front of every line. (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). So technically speaking the sed command will be in column 2 and the awk command will be column 1.

I would use another sed command but sed doesn't increment values as easily. 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

=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. Just use this awk script:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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