3

I want an batch scripting equivalent to this AWK script:

awk '{print $3}'

A file has this content:

XXX YYY : 8
Rrr rrr : 7
ddd rrr : 9

I want to get a batch script to print

8, 7, 9

Is there any way by which each value can be assigned to a loop and redirected to if condition?

Sree
  • 399
  • 2
  • 5
  • 12
  • If I understand correctly, this question should cover it? http://stackoverflow.com/questions/5536018/how-to-get-match-regex-pattern-using-awk-from-file – Christian Oct 08 '12 at 06:30

5 Answers5

8

I can't tell if your asking for a Windows Batch script or an awk script, so here's both:

test.txt:

XXX YYY : 8
Rrr rrr : 7
ddd rrr : 9

AWK:

awk 'NR > 1 {printf ", "}{printf $4}END{printf "\n"}' test.txt

Output:

8, 7, 9

Windows Batch script:

@echo off
SET _c=
FOR /F "tokens=4 delims= " %%G IN (test.txt) DO (
    IF DEFINED _c <nul set /p z=", "
    <nul set /p z=%%G
    SET _c=1
)

Output:

8, 7, 9
j.w.r
  • 4,136
  • 2
  • 27
  • 29
2
awk -F: '{x=x","$2;}END{print substr(x,2,length(x)-1)}' your_file

tested below:

> cat temp
XXX YYY : 8
Rrr rrr : 7
ddd rrr : 9
> awk -F: '{x=x","$2;}END{print substr(x,2,length(x)-1)}' temp
 8, 7, 9
Vijay
  • 65,327
  • 90
  • 227
  • 319
1

Another solution:

 awk '{ if(NR<3) printf "%s, ", $4 }END{ print $4 }' file

Result:

echo -e "XXX YYY : 8\nRrr rrr : 7\nddd rrr : 9" | awk '{ if(NR<3) printf "%s, ", $4 }END{ print $4 }'
8, 7, 9
Tedee12345
  • 1,182
  • 4
  • 16
  • 26
  • Why the if-condition? You can just `print ""` in `END` for the empty line. It will also be more general. – Bernhard Oct 08 '12 at 07:21
0

This awk '{print $3}' will print a list of : from your sample file, not the numbers. You should have used $4. Or, you are showing us the wrong file format here.

The exact Windows equivalent of that Awk script is:

FOR /F "tokens=4" %%a IN (file.txt) DO ECHO %%a

But then the required output you showed 8, 7, 9 isn't what your Awk script prints either. The simplest batch command producing output close to that is:

FOR /F "tokens=4" %%a IN (file.txt) DO <nul SET /p="%%a, "

If you really can't stand the extra comma in the end, it gets more complicated:

SETLOCAL
FOR /F "tokens=4" %%a IN (file.txt) DO <nul CALL SET /p=%%,%% %%a& SET ,=,

AWK on Windows

By the way, Awk is available for Windows. Quoting is different, so your script would be:

awk "{printf $4 \", \"}" file.txt

Or to get rid of that extra comma, it becomes slightly more messy:

awk "{printf (NR>1?\", \":\"\") $4}" file.txt
Amit Naidu
  • 2,494
  • 2
  • 24
  • 32
0

OUTPUT TO THE TERMINAL DIRECTLY

If you already have awk.exe try step (3) first before attempting (1:3)

AWK was superseded by GAWK which is also backwards compatible (Check (3) ).

  1. Get the Right GAWK

    So I used Gawk v3.6.1 running the setup link. Run the gawk-3.1.6-1-setup.exe.

    During install note the path to the directory you extract/install to, check mark both Binaries & Documentation & the rest is your preference.

  2. Extract GAWK

    Once installed, extract gawk.exe from the install directory by navigating to the path you noted in install & going to the bin directory. Copy gawk.exe then navigate to the directory of the batch file (*.bat) you wish to give gawk functionality & paste it (since its a portable *.exe & the purpose of my batch needs to be self contained).

  3. Use the Right Commands

    These commands print directly to the terminal nicely formatted the keyword I was missing took an entire day to find scouring the internet, then reading a text file almost 2000 lines long & finding BEGIN before the braces. It didn't occur to me until reading the gawk.1.txt in *\man\cat1

    gawk "BEGIN { print \"Hello world\"}"
    
    REM If you have printf from GNUwin32
    gawk "BEGIN { printf \"Hello world\"}"
    
nimig18
  • 797
  • 7
  • 10