Link to home
Start Free TrialLog in
Avatar of decoded
decodedFlag for United States of America

asked on

Script to edit text files

Hello Experts,

I am newbie when it comes to scrpting, is there a way to edit/erase this first 14 characters of a certain text file.

THanks in advance
Avatar of KruglerD
KruglerD
Flag of United States of America image

Batch, Powershell, Kixtart, VBScript?
Avatar of decoded

ASKER

Batch
Avatar of Brent Challis
Is there a specific reason for not using PowerShell as it would certainly be much easier?  I have written text replacement utilities for cmd.exe in the past, they compiled utilities though.  Functionality to achieve this is baked in to PowerShell.
Avatar of decoded

ASKER

I have very little expereince using powershells. If you give me a breif explanation i would probably get it.

Thanks
Avatar of decoded

ASKER

Can yuo assist me with the powershell...it does look it would be alot easier.

Thanks
Try this batch file:
@echo off
if "%~1"=="" echo Usage: %0 filename.txt & exit /b
if not exist "%~1" echo Cannot find file & exit /b

set /p line=<"%~f1"
echo.%line:~14%>"%temp%\%~nx1.tmp"
more +1 "%~f1">>"%temp%\%~nx1.tmp"
move "%temp%\%~xn1.tmp" "%~f1""

Open in new window

NOTE: Copy & Paste the code into Notepad and save it as a .BAT file. Run the batch file specifying a filename as a command line parameter like this:

    BATCHFILE filename.txt

Where 'BATCHFILE' is the name of your batch file and 'filename.txt' is the name of your text file.
Avatar of Bill Prew
Bill Prew

Do you want to remove the 14 characters from the file NAME, or from the file CONTENTS?

If CONTENTS, then do you want to remove the first 14 characters from just the first line, or every line?

~bp
decoded

Please permit me to draw your attention to my earlier comment http:#37782035 - the solution to this question.

Thnk you.
Avatar of decoded

ASKER

i used perl to get this done...wrote some lines of code and Mission accomplished

while(<*.dat>) {
    open(IN, $_) or die ("Cannot open $_: $!");
    my @in = <IN>;
    close(IN);
    shift @in;

    open(OUT, ">new-$_") or die ("Cannot open $_.new: $!");
    print OUT @in;
    close(OUT);
}



thanks
Avatar of decoded

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for decoded's comment #37858196

for the following reason:

Most reliable way to do it...doesn't cause orginal file to get damage. With my sloution it creates a new simple file.
ASKER CERTIFIED SOLUTION
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of decoded

ASKER

This soultion works as well...sorry for not taking a further look into you soultion...in no way I was trying to cheat.

Great Job
paultomasi