Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Question 1

Status
Not open for further replies.

martinjburgess

IS-IT--Management
Jun 8, 2012
21
GB
Hi,

I have a folder with a list of files - 1.dat, 2.dat, 3.dat etc
The file structure is:

rndsid = |11026|1342296563|
lastactive = |1342296563|
lastvisit = |1340862364|
sitename = |Free PC Help|
email = |admin@mysite.com|
pmpopup = |1|
lastpost = |1340736611|
password = |******|
posts = |2|
registered = |1339327069|
dateformat = |0|
timezone = |0|
timeformat = |0|
sn = |Administrator|
siteurl = |
What I need to do is read the content of each .dat file and remove the entries for siteurl and sitename if they exist.

Is this possible?

Many thanks
 
That's correct, if sn's value is administrator then leave the siteurl and sitename intact.
 
Hi

Well, this can be essentially done in two different ways :
[ul]
[li]Single pass : Process the file as now, but if sn = Administrator line is met, cancel the processing.[/li]
[li]Two passes : Read the file and check the existence of sn = Administrator line. If non find, process the file as now.[/li]
[/ul]

Probably I should have asked this earlier. The dat files will be all like that sample, with 10~20 lines, under 1Kb size ? If yes, we can just slurp in the entire file instead of processing line by line. This way neither temporary file and file moving is involved :
Perl:
[b]undef[/b] [navy]$/[/navy][teal];[/teal]

[b]foreach[/b] [navy]$file[/navy] [teal]([/teal][green][i]<*.dat>[/i][/green][teal])[/teal] [teal]{[/teal]
  [b]open[/b] FIL[teal],[/teal][green][i]"<$file"[/i][/green][teal];[/teal]
  [navy]$data[/navy][teal]=[/teal][green][i]<FIL>[/i][/green][teal];[/teal]
  [b]close[/b] FIL[teal];[/teal]

  [b]next[/b] [b]if[/b] [navy]$data[/navy][teal]=~[/teal][b]m[/b][fuchsia]/^sn\s*=\s*\|Administrator\|/[/fuchsia]mi[teal];[/teal]

  [navy]$data[/navy][teal]=~[/teal][b]s[/b][fuchsia]/^site(url|name)\s*=.*\r?\n?//[/fuchsia][b]gm[/b][teal];[/teal]

  [b]open[/b] FIL[teal],[/teal][green][i]">$file"[/i][/green][teal];[/teal]
  [b]print[/b] FIL [navy]$data[/navy][teal];[/teal]
  [b]close[/b] FIL[teal];[/teal]
[teal]}[/teal]

Feherke.
[link feherke.github.com/][/url]
 
That last code completely mucks up the table layout of my test forum for some reason.

These dat files are only small, never seen one larger than 1Kb
 
Hi

I assume is because my ugly habit to unset the [tt]$/[/tt]. This should play nicely :
Perl:
[highlight][gray]#undef $/; # <-- remove this one[/gray][/highlight]

[b]foreach[/b] [navy]$file[/navy] [teal]([/teal][green][i]<*.dat>[/i][/green][teal])[/teal] [teal]{[/teal]
  [highlight][b]local[/b] [navy]$/[/navy][teal];[/teal][/highlight]

  [gray]# ...[/gray]
[teal]}[/teal]

Feherke.
[link feherke.github.com/][/url]
 
That's done the trick, I'll do some more testing and let you know.
Thank you, much appreciated.
 
Sorry it's late but just want to say thanks to feherke for his patience and knowledge. Everything I wanted to achieve I have.
Thank You
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top