Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

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
0
hgj1357
Asked:
hgj1357
  • 4
  • 2
  • 2
1 Solution
 
SunBowCommented:
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.
0
 
SunBowCommented:
> 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
0
 
hgj1357Author Commented:
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
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
SunBowCommented:
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.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
I believe this should do it - tests seem ok (it's different from my first post)

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

What this will do is make a backup folder if there isn't one.
Then look for all .tfw files and for each one,
it will prepend first4.txt to the file.tfw.tmp
Then it will read over the file.tfw, skipping the first four lines and writing back all lines after it, effectively recreatng the file but with a new set of first 4 lines.
Then it will move the original file into the backup folder,
and rename the tmp file to the original file name.
0
 
SunBowCommented:
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)
0
 
hgj1357Author Commented:
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.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now