Jump to content

XP Batch file help need


Recommended Posts

This command:

mawk -F\t "$7 ~ /agin/ {print NR}" v.tsv

has this 3 line output:

6

8

24

I want to run these three commands:

mawk -f record.awk -v rec=6 v.tsv

mawk -f record.awk -v rec=8 v.tsv

mawk -f record.awk -v rec=24 v.tsv

How do I do this in a for loop? This is what I have tried:

for /F "usebackq" %%R in (`mawk -F\t "$7 ~ /agin/ {print NR}" v.tsv`) do mawk -f record.awk -v rec=%R v.tsv

to debug, I am using:

for /F "usebackq" %%R in (`mawk -F\t "$7 ~ /agin/ {print NR}" v.tsv`) do @echo %R

which has the output of:

R

R

R

I have also tried this:

for %%R in ('mawk -F\t "$7 ~ /agin/ {print NR}" v.tsv') do @echo %R

which outputs:

R

R

R

R

R

R

I have also tried single-quotes & back quotes to no avail and also fiddled around with the for command, but can not get this working.

How can I make a for loop to run this command 3 times with # being replaced with 6, 8, and 24...

mawk -f record.awk -v rec=# v.tsv

I hope this makes sense,

-John

Link to comment
Share on other sites


Perhaps this will work:

for /f "delims=;" %%i in (`mawk -F\t "$7 ~ /agin/ {print NR}" v.tsv`) do (mawk -f record.awk -v rec=%%i v.tsv)

But i am pretty the following will work:

mawk -F\t "$7 ~ /agin/ {print NR}" v.tsv >%temp%\temptxt.txt
for /f "delims=;" %%i in (%temp%\temptxt.txt) do (mawk -f record.awk -v rec=%%i v.tsv)

Link to comment
Share on other sites

allen2,

Thanks, I got it working with this:

for /f "usebackq delims=;" %%i in (`mawk -F\t "$7 ~ /agin/ {print NR}" v.tsv`) do mawk -f record.awk -v rec=%%i v.tsv

In this context what does delims=; do? I assume it tells the for loop that there are no delimiters? If this is the case, I still don't understand why this makes it work. Can you explain this?

Thanks,

-John

Link to comment
Share on other sites

The default delimiter is space so setting it to something that isn't in the lines will force %%i to take each entire line.

Interesting, but not really needed there are standard ways to do it:

@echo off

ECHO Standard way one

for /F "tokens=*" %%A in ('dir *.cmd') do echo %%A

pause

ECHO Standard way two

for /F "delims=" %%A in ('dir *.cmd') do echo %%A

pause

Echo allen2's way

for /F "delims=;" %%A in ('dir *.cmd') do echo %%A

@jftuga

For future use, you are getting the R's because the underlined part is wrong:

for /F "usebackq" %%R in (`mawk -F\t "$7 ~ /agin/ {print NR}" v.tsv`) do @echo %R

The variable is %%R, not %R:

for /F "usebackq" %%R in (`mawk -F\t "$7 ~ /agin/ {print NR}" v.tsv`) do @echo %%R

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...