?
Solved

Script to edit text files

Posted on 2012-03-28
15
Medium Priority
?
526 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Brent Challis
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
LVL 56

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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

770 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