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
Solved

Script to edit text files

Posted on 2012-03-28
15
521 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: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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 53

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

808 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