1

I am trying to append /32 to public IP that is in a file using awk

ip.txt has IP address 4.14.XX.XXX

the issue is with the command in script file

awk "{print $0 "/32"}" < ip.txt

result=0.129375

if I remove / than the result is 4.14.XX.XXX32

I want 4.14.XX.XXX/32.

The complete bat file

curl v4.ifconfig.co > ip.txt
awk "{ print $0 "/32" }" < ip.txt > ipnew.txt
@REM set stuff=type ipnew.txt
for /f %%i in ('type ipnew.txt') do set stuff=%%i
echo %stuff%

====================

awk -f changeipaddress.bat

error

awk: changeipaddress.bat:1: curl v4.ifconfig.co > ip.txt
awk: changeipaddress.bat:1:        ^ syntax error
awk: changeipaddress.bat:1: curl v4.ifconfig.co > ip.txt
awk: changeipaddress.bat:1:                         ^ syntax error
awk: changeipaddress.bat:2: awk "{ print $0 "/32" }" < ip.txt > ipnew.txt
awk: changeipaddress.bat:2:                              ^ syntax error
awk: changeipaddress.bat:2: awk "{ print $0 "/32" }" < ip.txt > ipnew.txt
awk: changeipaddress.bat:2:                                          ^ syntax error
awk: changeipaddress.bat:3: @REM set stuff=type ipnew.txt
awk: changeipaddress.bat:3: ^ invalid char '@' in expression

Please suggest where I am going wrong?

Regards, TJ.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Tarak
  • 65
  • 13
  • 1
    use single quotes to enclose the script... `awk '{print $0 "/32"}'` ... see [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – Sundeep Jul 02 '18 at 15:25
  • 1
    on noticing `window` in title... may be you can't use single quotes.. please add appropriate tag to get better help – Sundeep Jul 02 '18 at 15:28
  • Possible duplicate of [How to run an awk commands in windows?](https://stackoverflow.com/questions/21927944/how-to-run-an-awk-commands-in-windows) – JNevill Jul 02 '18 at 16:53
  • Answer in there about how to escape quotes and the issues like that you can run into when running awk for windows. – JNevill Jul 02 '18 at 16:53
  • @EdMorton from CMD prompt in MS – Tarak Jul 02 '18 at 17:23
  • @Sundeep i am not understanding about tag to add. In the question, I have added double quote and that is why its giving result but wrong value. My issue is when using backward slash and how to solve it. – Tarak Jul 02 '18 at 17:27
  • @EdMorton actually my script file have some more command line argument like curl and set commands. so it failing while coming to awk. – Tarak Jul 02 '18 at 17:29
  • @EdMorton I tried running as suggested but getting systax error example awk: changeipaddress.bat:1: curl v4.ifconfig.co > ip.txt – Tarak Jul 02 '18 at 17:32
  • @EdMorton.. Just updated the thread. Hopefully that give more details – Tarak Jul 02 '18 at 17:39
  • 2
    @EdMorton thank you!! it's working finally. Learned new lesson and how to use the forum. Thank you again for the explanation. – Tarak Jul 02 '18 at 17:58

1 Answers1

1

Create a file named foo.awk with content { print $0 "/32" } (i.e. the awk script) then change line 2 of your bat file from awk "{ print $0 "/32" }" < ip.txt > ipnew.txt to awk -f foo.awk < ip.txt > ipnew.txt. Now run your bat file however you normally do.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185