Deletion of last line in a File & adding a new last line

I am having a file named Trade.txt having n number of lines.
I need the last line to get deleted ( The last line is the string "Report type" ) & I need to add new characters to the last line.
The new character is "Trans Date".

Please help me somebody as I am new to scripting
Ragaz MaheAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Give this a try, adjust the file names near the top...

@echo off
setlocal

set FileIn=in.txt
set FileOut=out.txt

find /v /i "report type" <"%FileIn%" >"%FileOut%"
echo Trans Date>>"%FileOut%"

Open in new window


»bp
1
aikimarkCommented:
@Ragaz

Is the last line the only place the "Report type" string will ever appear in the file?
0
Ragaz MaheAuthor Commented:
@aikimark, Its like anywhere in the file the string "report type" would be present. But I need the string in the last line alone to be replaced.
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Ragaz MaheAuthor Commented:
@Bill Prew,  Kindly  be noted that, the said string "report type" would be anywhere in the file. But I need the string in the last line alone to be replaced.
0
Bill PrewIT / Software Engineering ConsultantCommented:
That's what my script did.


»bp
0
Ragaz MaheAuthor Commented:
@Bill Prew,

I have tested your code, I could see that the last line is getting replaced with Trans date. At the same time, the string(report type) that is present in all the other places other than the last line is getting disappeared in the output file. Please advise
0
aikimarkCommented:
Can you run Powershell or VBscript scripts?
0
Ragaz MaheAuthor Commented:
@aikimark, I am sorry, but i need to run through a BAT or PERL Script
0
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, give this a try please.

@echo off
setlocal EnableDelayedExpansion

rem Specify input and output files
set FileIn=in.txt
set FileOut=out.txt

rem Specify text for repacement on last line
set ReplaceOld=Report type
set ReplaceNew=Trans Date

rem Delete output file if it exists
if exist "%FileOut%" del "%FileOut%"

rem Get line number of last line in file
set LastLine=0
for /f %%A in ('find /v /c ""^<"%FileIn%"') do set LastLine=%%A

rem Read each line of file, write to output file
for /f "tokens=1* delims=[]" %%A in ('find /v /n ""^<"%FileIn%"') do (
    set Data=%%B

    rem If last line of file, do replacement
    if %%A EQU %LastLine% set Data=!Data:%ReplaceOld%=%ReplaceNew%!

    echo.!Data!>>"%FileOut%"
)

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Bat

From novice to tech pro — start learning today.