Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK equivalent in batch scripting

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?

like image 847
Sree Avatar asked Dec 06 '25 04:12

Sree


1 Answers

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
like image 190
j.w.r Avatar answered Dec 09 '25 15:12

j.w.r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!