read the last two words of each line

October 11, 2011 at 07:45:43
Specs: DOS
following is the temp.txt data
TO M/s. ABC OFFSET Challan No.: R 1397
TO M/s. test OFFSET, MUMBAI Challan No.: R 5527
TO M/s. test OFFSET DATE: 11-10-2011 Challan No.: T 1987

i want the last two words of each line "R 1397", "R 5527" " T 1987" from above temp.txt file as "R1397" (in temp.txt there is space between "R 1397")

i am using windows XP, but i need the solution in batch file (through dos commands)


note: want the result in batch file (through dos command)


See More: read the last two words of each line

Report •


#1
October 11, 2011 at 09:55:59
Hi Sujeet
Double posts are frowned upon on this site,
I assume your using the dos prompt in Windows XP
If so here are two versions
The first Version assumes there is always one letter and 4 digits
If not the second Version will work regardless of how many letters and numbers.
The data is written to the Temp.txt file.
Hope this helps

@echo off
cls
type nul > Temp.txt
rem Version 1
SetLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in (Data.txt) do (
set Tokens=%%a
echo !Tokens:~-6!>>Temp.txt
)

echo.
---------------------------------------------------
rem Version 2
SetLocal EnableDelayedExpansion
type nul > Temp.txt
for /f "tokens=* delims=" %%a in (Data.txt) do (
call :GetTokens %%a
echo !Token1! !Token2!>>Temp.txt
)
exit /b

:GetTokens %*
:Again
set Token1=%1
set Token2=%2
shift
if "%1"=="" (exit /b)
if "%2"=="" (exit /b)
goto Again


Report •

#2
October 11, 2011 at 23:35:12
thanks for the answer, very near to result

following result is found in temp.txt from : version 2
R 1397
R 5527
T 1987

i need the result as under (for example: no space between R and 1397)
it must be like under :
R1397
R5527
T1987

i want final output : rename the INVOICE.TXT file with result R-01397.TXT

ren invoice.txt R-01397.txt

waiting for your kind reply


Report •

#3
October 12, 2011 at 10:41:59
Hi Sujeet
Use version and delete the space between the two tokens
ie.
echo !Token1! Token2!>>Temp.txt

Not sure what you mean by renaming the Invoice.txt file.
Do you want the output that gos to the Temp.txt go to file
of this name and how do your get what to rename this file is it always
renamed to R-01397.TXT, if so the output could go to a file of this name.


Report •

Related Solutions

#4
October 12, 2011 at 22:33:22
thanks for your suggestion, i will try

Report •

#5
October 14, 2011 at 07:21:18
I have tried with version 2, but with following data the result comes like below

Result: "-.txt (file generated)

------------------------
following is the temp.txt data
TO M/s. ABC OFFSET Challan No.: R 1239
TO M/s. JAIN & CO. Challan No.: R 2267
TO M/s. test OFFSET DATE: 11-10-2011 Challan No.: T 4428
TO M/s. ABC LTD Challan No.: T 4490
TO M/s. NIRAN ASSOCIATE & INC. DATEE: 11-10-2011 Challan No.: T 4428

With obove temp.txt, following result comes
R-1239.TXT
-.TXT
T-4428.TXT
T-4490.TXT
-.TXT

I have noticed that result not comes as per required where customer name is with "&"

Pls. do the needful and send the modified version 2



Report •

#6
October 15, 2011 at 11:20:30
Hi Sujeet
The ".TXT" is not in the file data so I guess your adding it somehow.
Do'es it work ok when theres no "&" in the data
.
This solve your problem
@echo off
SetLocal EnableDelayedExpansion
cls
type nul > Temp.txt
for /f "tokens=* delims=" %%a in (Data.txt) do (
set Tokens=%%a
set Tokens=!Tokens:~-6!
call :GetTokens !Tokens!
)
exit /b

:GetTokens %*
echo %1%2>>Temp.txt
exit /b


Report •

#7
October 18, 2011 at 22:00:33
thanks a lot, now result is 100% ok

Report •


Ask Question