batch file to print tokens of different lines

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Amin
Posts: 20
Joined: 30 May 2011 05:25

batch file to print tokens of different lines

#1 Post by Amin » 30 May 2011 05:53

Hi All
Can you please help me solve this issue:
I have a text file as follows (between the lines ------):

-------------------------------text file contents below--------------------
some code and code2 other
29 4 14 ddd 208 16/3 R2C
PRODNAM MANDATE SERNO MASTRP
neededx 11W04 TD3A057135 951
SLSTATE DATE TIME
0 110415 031735

some code and code2 other
29 4 13 ddd 208 16/3 R2C
PRODNAM MANDATE SERNO MASTRP
neededy 11W04 TD3A057068 951
SLSTATE DATE TIME
0 110415 031624

some code and code2 other
29 5 10 2/ddd 208 459/1 R4B
PRODNAM MANDATE SERNO MASTRP
neededz 11W05 TD3A076719 936
SLSTATE DATE TIME
0 110415 030400

some code and code2 other
29 4 14 ddd 208 16/3 R2C
PRODNAM MANDATE SERNO MASTRP
neededv 11W04 TD3A057135 951
SLSTATE DATE TIME
0 110415 031735

-------------------------------text file contents above--------------------


Note that the text file appears as repeated blocks and the heading of each block is the same.
i want to output the following :
- print the heading once
-under the heading :print the first 3 tokens from the second line, and the fourth token from the the fourth line of the block.

example(output required from obove file):
===============================

some code and code2 other
29 4 14 neededx
29 4 13 neededy
29 5 10 neededz
29 4 14 neededv


My trial to solve it:
=================
regarding the heading, since it is well known i can delete it and write it once, that's OK, then i deleted the lines that are not necessary (including heading) as follows:
type filename.txt | FIND/v "PRODNA" | FIND/v "SLSTAT" | FIND/v "some" >>file2.txt

so ,now i have the file as follows:


29 4 14 ddd 208 16/3 R2C
neededx
29 4 13 ddd 208 16/3 R2C
neededy
29 5 10 2/ddd 208 459/1 R4B
neededz
29 4 14 ddd 208 16/3 R2C
neededv

the remaining is the get the fourth token from the second line not the first one, this lead to thinking about coding based on the line number (odd,even) .....but i have no more success.

your help is appreciated ,regardless if built on my trial or not.
Regards

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: batch file to print tokens of different lines

#2 Post by amel27 » 30 May 2011 06:22

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion

set "fline=0"
(for /f "usebackq tokens=*" %%x in ("file1.txt") do (
  set/a "bline=1+fline%%6,block=1+fline/6,fline+=1"
  for /f "tokens=1-4" %%a in ("%%x") do (
    if !fline! equ 1 echo %%x
    if !bline! equ 2 set/p .="%%a %%b %%c "
    if !bline! equ 4 echo %%a
)))<nul >file2.txt

Amin
Posts: 20
Joined: 30 May 2011 05:25

Re: batch file to print tokens of different lines

#3 Post by Amin » 30 May 2011 06:34

Dear Amel27
Thank you very much for you help and fast reply, i tried it and it is working perfectly, this facilitates a routine job for me.
Thanks again and Best regards!
Amin

Post Reply