• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Replacing strings in text files

Dinh

Limp Gawd
Joined
Aug 27, 2006
Messages
194
I have a set of text files that have the content somewhat similar to the following :

Code:
header[00]
header[01]
header[02]
header[03]
header[04]

Now, lets say I have a few hundred lines going and I have to add one line. Then, I'll have to waste more renumbering everything.

The reason why? Its part of a javascript file for a header of a website. I only edit the html and know nothing about javascript.

I was thinking of going for something that replaces a certain character into an increasing number set. For example :

Code:
header[xx]<html>
header[xx]<head>
header[xx]<title>sfsjkfs</title>
header[xx]</head>
header[xx]<body>

I would need a solution that replaces the xx's with increasing numbers (1,2,3,4,5). I've looked up Bat scripting but have no luck. Can someone point me in the right direction?
 
sounds like a horrible design, are you really stuck working with this? granted, i dont know what the reason is for this explicit numbering.

i think perl is the best language for dealing with this. i might be able to whip something up, or at least let you know its feasible, but i would need the following clarification:

1. the lines that you're inserting, will they have a header[xx] is front of them as well?

2. are there multiple sequences of header[xx]'s in a particular file? i.e.

header[00]blah
header[01]blah
la la la
header[00]blah
...

3. will these added lines always be between a group of header[xx]'s, or might they come in at the end of a sequence?
 
sounds like a horrible design, are you really stuck working with this? granted, i dont know what the reason is for this explicit numbering.

i think perl is the best language for dealing with this. i might be able to whip something up, or at least let you know its feasible, but i would need the following clarification:

1. the lines that you're inserting, will they have a header[xx] is front of them as well?

2. are there multiple sequences of header[xx]'s in a particular file? i.e.



3. will these added lines always be between a group of header[xx]'s, or might they come in at the end of a sequence?


Yeah I know, its a horrible solution and much more work than a simple PHP include =/, but I'm stuck with it due to limited resources.

1. Every new line = header[xx] before it (basicly header[xx] before each line of html code).

2. Nope, theres only one huge sequence.

3. Those inserted lines can be anywhere, top or bottom. Its just renumbering is a pain the arse :eek:
 
2. Nope, theres only one huge sequence.
Then, isn't the [xx] number just the line number in the file? If so, isn't it trivial to regenerate it? You should be able to write a program that takes an input file like

Code:
Pittsburgh
Atlanta
New York

and produces the file you desire
Code:
Header[00]Pittsburgh
Header[01]Atlanta
Header[02]New York

in a couple dozen lines, right?
 
Oh yes, that is a much better idea. But I have no idea what to use.
 
ahh, so the entire file is consistently prefixed? no intermixed areas?

you can do this with a number of tools, sed could knock it out real easy but i dont know how to keep a counter as you iterate through the lines.

here's the jist of it with perl, seems like it should do what you need:

Code:
fluxion@purity:~/temp$ cat parseFile.pl 
#!/usr/bin/perl

my $c = 0;

while (<>) {
  s/^header\[\d+\]/header[$c]/;
  print $_;
  ++$c;
}
  
fluxion@purity:~/temp$ cat file 
header[34]blah1
header[1]blah2
header[2]blah3
header[5]blah4
header[8]blah5
fluxion@purity:~/temp$ cat file | ./parseFile.pl 
header[0]blah1
header[1]blah2
header[2]blah3
header[3]blah4
header[4]blah5
fluxion@purity:~/temp$
 
Oh yes, that is a much better idea. But I have no idea what to use.
I don't think the language really matters. Use whatever you know.


Here's a one-liner in perl that does it:
Code:
> cat cities 
Pittsburgh
Atlanta
New York

> cat cities | perl -pe '$i or $i = 0; s/^/header[$i] /; $i++'
header[0] Pittsburgh
header[1] Atlanta
header[2] New York

If you need to format the numbers to two digits then it become slightly more complex.
 
Back
Top