Solved

Script to edit text files

Posted on 2012-03-28
15
518 Views
Last Modified: 2012-04-18
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
0
Comment
Question by:decoded
15 Comments
 
LVL 3

Expert Comment

by:KruglerD
ID: 37779552
Batch, Powershell, Kixtart, VBScript?
0
 

Author Comment

by:decoded
ID: 37779559
Batch
0
 
LVL 8

Expert Comment

by:bchallis
ID: 37780119
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.
0
 

Author Comment

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

Thanks
0
 

Author Comment

by:decoded
ID: 37782035
Can yuo assist me with the powershell...it does look it would be alot easier.

Thanks
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37788226
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.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 52

Expert Comment

by:Bill Prew
ID: 37791976
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
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37800187
decoded

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

Thnk you.
0
 

Author Comment

by:decoded
ID: 37858196
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
0
 

Author Comment

by:decoded
ID: 37858656
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.
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 500 total points
ID: 37858657
Something strange going on here...

decoded

In my comment (http:#37800187) above where I wrote:    
Please permit me to draw your attention to my earlier comment http:#37782035
Did a comment ID37782035 get deleted or something, or did I missquote the comment ID number?

Did my solution in http:#37788226 (above) not work then?

Incidentally, you didn't say anything about not overwriting the original file. You actually stated:
...is there a way to edit/erase this first 14 characters...
Using the relevent lines of code from my comment above, and omiting the last line (Line 8), you would end up with a new file named 'new-filename' (whatever your filename is). Please see the code below:
@echo off
if "%~1"=="" echo Usage: %0 filename & exit /b
if not exist "%~1" echo Cannot find the file %~1 & exit /b
set /p line=<"%~f1"
echo.%line:~14%>"new-%~nx1"
more +1 "%~f1">>"new-%~nx1"

Open in new window

I also feel this question, and solution has value. It's not something that crops up frequently enough to justify discarding.

I feel a little bit cheated as decoded has moved the goalposts without prior notice, and after a working solution to the question had already been submitted.
0
 

Author Closing Comment

by:decoded
ID: 37861707
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now