Link to home
Start Free TrialLog in
Avatar of hgj1357
hgj1357

asked on

Change the first 4 lines of multiple text files.

I have around 200 text files that I need to modify. The first four lines all need to be cahnged to the same new four lines. I'd like to be able to wildcard (*.TFW) rather than attempt to generate a list of each file.

Many thanks
Avatar of SunBow
SunBow
Flag of United States of America image

Change can be a modify or a delete & add operation.

To add, you can have a single file with only the four lines, then copy the two together to make a new file, or to overwrite. New file sounds best, using append.

I cannot think offhand off deleting the four lines. But if this is for designing up front, ahead of time, and the four lines are for some internal standard like having the title for the system and a datestamp, then replacing them periodically in some common files like log files, the process can use a different design.

Namely, keep the new files always separate, and periodically make a separate single file with the four lines. It could be good idea to use separate directory for the copies. Use the append operation as above, like with a copy command. A good follow up would be to then move those files to a new directory to signify operation completed, for tracking, for segregating & distinguishing files, and to minimize any need to tinker with filenames.

Dos is like Windows, they are operating systems, good at handling files and moving them around. Operating Systems are not good at messing around with file internals, for line editing. Editors are better for editing.
> rather than attempt to generate a list of each file

For this, again my preference is to segregate them by directory. Then you won't need a filter, you can just do all files found there. You can copy or move them there, or keep them there. The directory itself can then contain some of the desired supplemental information, if not the entire four lines to replace. For example, for a monthly backup, the name of the directory could be the month. Should one line be company and another the person performing the backup, these could all be also placed in the directory name, ex: MyCompany, July Backup by Greg on August 1, 2006
Avatar of hgj1357
hgj1357

ASKER

I don't care how I do it.

files are:

22482250H.tfw
22522250H.tfw   etc etc

first four lines are

0.5
0.0
0.0
-0.5


I want them to be

1.0
0.0
0.0
-1.0

I need simple insturctions I can follow
How Unique are the lines - can search and replace do? Personally, I think if you are doing that kind of thing on a more regular basis you should get copies and experience with the unix alternatives. But here's a quick response from google on a couple tools, one to do line deletion (see above), the other to do a simple character translation, (which can include the three line terminators) if you know enough about file contents to know they would not be repeated after line 4:

http://short.stop.home.att.net/freesoft/txtutil3.htm#linekill

LINE KILL / REPLACE

--------------------------------------------------------------------------------

KLine — Delete lines of text containing search string.

* * *

KLine deletes entire lines which contain a search string. Accepts wildcards and creates a backup of original file. Outputs the number of lines killed per file. Case sensitive/insensitive options.

Limitation: KLine will truncate lines longer than 255 characters.

Usage: KLine text_file(s) "offending string" [/c (case sensitive)]
Author: David Daniel Anderson / Reign Ware (1994).

Download kline101.zip (13K).


--------------------------------------------------------------------------------

YANK — Replace or delete lines of text containing search string.

* * * *

[added 1998-04-19, updated 2000-03-02]

Like KLine, this program can be used to strip out entire lines containing a search string, but it also offers the option of replacing the line with another string, or an image file's content, rather than deleting it. Search and replace strings can be any combination of literals and ASCII codes. Other differences from KLine: 1) YANK handles longer lines; 2) does not create a backup file; 4) "can read from another file both the search and replace string specifications or replacement text image (65,534 char. max; search string limit 4,095 char.)."

Limitations: Unlike KLine, YANK doesn't output number of changes made per file. Works fine on my MS-DOS machine – but can't get it to work properly under OpenDOS.

YANK [/i] filespec "[srchstrng][^[x]chr,][...]" ["[replstrng][^[x]chr,][...]"]
YANK [/i] filespec @scriptfile or YANK [/i] filespec "srchspec" @imagefile
i...case insensitive
Author: Clay Ruth (1996, 2000). Suggested by Marianna Van Erp.

2000-01-14: v1.3, part of Clay's Utilities for MS-DOS.

Download cutpak13.zip (398K).

Go to Clay's Utilities for DOS Batch Processes for upgrades of some other utils in the package, Windows version, info, license agreement & more.
Avatar of Lee W, MVP
Make a file called "first4.txt" and put in that file the four lines you WANT in the file.  Then put this in a batch file.

if not exist .\backup md .\backup
for /f "tokens=*" %%a in ('dir *.tfw /b') do (
    type %%a > %%a.tmp
    for /f "tokens=* skip=4" %%m in (%%a) do echo %%m >> %%a.tmp
    move %%a .\backup
    ren %%a.tmp %%a
)


Double checking and testing this now
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America 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
Clarification for earlier comment on unix:

For unix, there is Grep & Egrep (and similar F-Grep, A-Grep), Awk and Gawk, and Sed, etc.

These are all now available for dos in many forms, for many windows versions, just more available via web than MS, although MS used to have some on their resource kits. The latter is one drawback, windows (version changes), another is that some programs are shareware, not free, and .... you/we cannot count on any of them being complete implementations of the original, or any two of them alike in capability. Yet they are useful, and should be part of a toolkit for people managing files like that on a frequent basis.

Samples:
http://www.tldp.org/LDP/abs/html/textproc.html
ex: "cut"

Other handy tools would be Rexx (editing) and KiXtart (free dos enhancements for windows, used to be on MS Resource Kit for fee)
Avatar of hgj1357

ASKER

leew, how do you get to know this stuff?

Excellent!

Two comments:

A  Backup folder is an excellent idea
B  First4.txt needs a carraige return after the last line

Works like a charm

Thanks for your input SunBow but I think your ideas were more suited to a Linux environment.