1

I am currently trying to add a new string on the beginning of each line in a text file (temp.txt) and make a new file with combined text (newtemp.txt) using awk.

My code is:

awk  " NR>1{printf \"=HYPERLINK(B%d, C%d) "\t" https://otrs.city.pittsburgh.pa.us/index.pl?Action=AgentTicketZoom;TicketID=%s\n\", NR-1, NR-1, $0}" temp.txt > untemp.txt

It is making a new file called untemp.txt that only has the string.
EX:

--------------------------------------------------------------------------------
=HYPERLINK(B2, C2)  https://otrs.city.pittsburgh.pa.us/index.pl?Action=AgentTicketZoom;TicketID=
--------------------------------------------------------------------------------
=HYPERLINK(B3, C3)  https://otrs.city.pittsburgh.pa.us/index.pl?Action=AgentTicketZoom;TicketID=
--------------------------------------------------------------------------------
=HYPERLINK(B4, C4)  https://otrs.city.pittsburgh.pa.us/index.pl?Action=AgentTicketZoom;TicketID=
etc...

I want it to add the string above at the beginning of every line in the temp.txt.
It works in gawk but not awk.
What is the correct syntax, I am using Linux shell.

Thanks for the help!

3
  • 2
    +1 for apparently trying to manipulate spreadsheets with awk. Commented Jul 9, 2014 at 16:21
  • 1
    awk ' NR>1{printf "=HYPERLINK(B%d, C%d) \t https://otrs.confidental/index.pl?Action=AgentTicketZoom;TicketID=%s\n", NR-1, NR-1, $0} ' temp.txt > newtemp.txt worked for me on Mac Commented Jul 9, 2014 at 16:23
  • Yup, and it worked for me in gawk(windows GNU shell) but not awk (Linux shell) I edited the form, tell me if it works now and reread Commented Jul 9, 2014 at 16:26

1 Answer 1

1

I don't think your original string,

" NR>1{printf \"=HYPERLINK(B%d, C%d) "\t" https://otrs.city.pittsburgh.pa.us/index.pl?Action=AgentTicketZoom;TicketID=%s\n\", NR-1, NR-1, $0}"

, would work as an awk program in any (Bourne-like) shell:

Your string is composed of 3 separate strings joined to form a single string:

  • Part 1: " NR>1{printf \"=HYPERLINK(B%d, C%d) "
    • This part will happens to be passed through to awk unmodified - see part 3 below.
  • Part 2: \t is an unquoted string that does NOT represent a char; instead, the shell interprets this as just t (it "eats" the \), and only t gets appended to part 1.
  • Part 3: " https://otrs.city.pittsburgh.pa.us/index.pl?Action=AgentTicketZoom;TicketID=%s\n\", NR-1, NR-1, $0}" - due to using double quotes - is expanded by the shell first, so before awk even sees the string, the shell expands $0 to its definition of that token (loosely speaking, the shell executable in an interactive shell, the script filename in a script).

When run from an interactive (non-login) bash shell, this is what awk would see (broken into multiple lines just for readability - note the lone t and bash in lieu of $0):

 NR>1{printf "=HYPERLINK(B%d, C%d) t 
 https://otrs.city.pittsburgh.pa.us/index.pl?Action=AgentTicketZoom;TicketID=%s\n", 
 NR-1, NR-1, bash}

Clearly not what you had intended.


The fix, as already posted in a comment by @GolgiDevang, is to use single quotes around the entire string, which is generally the best choice for awk programs:

  • It prevents the shell from applying undesired expansions before the string is passed to awk.
    • With double quotes, the potential for confusion is great, because $-prefixed tokens have meaning both in the shell and in awk.
    • If you do need to pass shell variables for use inside an awk program, use the -v option; e.g.: -v var="$var" - this defines awk variable var with value of shell variable $var
  • It allows you to use " without escaping (and if you do need to escape nested double quotes awk's sake, use \").

Thus (did you mean to surround the with a space each?):

awk 'NR>1{printf "=HYPERLINK(B%d, C%d) \t https://otrs.city.pittsburgh.pa.us/index.pl?Action=AgentTicketZoom;TicketID=%s\n", NR-1, NR-1, $0}' temp.txt > untemp.txt
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.