Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie question

Status
Not open for further replies.

GRTman

Programmer
Feb 3, 2024
2
GB
I've downloaded gawk 3.1.6 and started using awk at the CMD prompt.

However I have a mysterious problem:

My input file tt.tx contains:
AAA,BBB
CCC,DDD
EEE,FFF

When I run this script it ignores the text "test". I cant work out why?

Correction:

C:\Users\md\Desktop>awk -F, "{nvar="test"; print $1 nvar $2}" tt.txt
AAABBB
CCCDDD
EEEFFF

 
Hi

That is CMD problem, not Awk problem. You need to fix your quoting :
Code:
[ ]       [gray]╭─pair─╮    ╭───────pair────────╮[/gray]
        [gray]▼      ▼    ▼                   ▼[/gray]
awk -F, [highlight palegoldenrod][green]"[/green]{nvar=[green]"[/green][/highlight]test[highlight palegoldenrod][green]"[/green]; print $1 nvar $2}[green]"[/green][/highlight] tt.txt

In Linux shell the solution is to enclose the Awk code in single quotes ( ' ), but as far as I know, that possibility does not exist in CMD. In which case your only solution is to escape all double quotes ( " ) inside the Awk code. I have some ancient memories about the escape character being the caret ( ^ ), but no idea whether is valid today and in your case :
Code:
awk -F, "{nvar=[red]^[/red]"test[red]^[/red]"; print $1 nvar $2}" tt.txt

Alternatively, especially if the code is longer and frequently needed, you can put it in a file :
Code:
[teal]{[/teal][navy]nvar[/navy][teal]=[/teal][i][green]"test"[/green][/i][teal];[/teal] [b]print[/b] [navy]$1[/navy] nvar [navy]$2[/navy][teal]}[/teal]
Code:
awk -F, -f GRTman.awk tt.txt

But if you choose this later alternative, better move the [tt]-F[/tt] option inside the script file, for consistency :
Code:
[b]BEGIN[/b] [teal]{[/teal]
    [navy]FS[/navy][teal]=[/teal][i][green]","[/green][/i]
[teal]}[/teal]

[teal]{[/teal]
    [navy]nvar[/navy][teal]=[/teal][i][green]"test"[/green][/i]
    [b]print[/b] [navy]$1[/navy] nvar [navy]$2[/navy]
[teal]}[/teal]
Code:
awk -f GRTman.awk tt.txt


Feherke.
feherke.github.io
 
Thanks! that solved my problem. Another solution I found is to put the awk parameters into a script file. Then it sems you dont have to worry what the CMD interface is doing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top