Solved

Script to edit text files

Posted on 2012-03-28
15
515 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 51

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
WPF issue with Trigger 2 66
how to add field in my script 2 24
Need a good Angular tutorial 5 28
Base1 Encode/Decode 3 30
The purpose of this article is to demonstrate how we can use conditional statements using Python.
A short article about a problem I had getting the GPS LocationListener working.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now