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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unlink files based on certain criteria

Status
Not open for further replies.
Jun 3, 2007
84
US
Hello,

Wondering if someone could please point me in the right direction. I have a file like the one shown below which is separated by "---------" between filenames/paths. Now what I want to do is remove/delete each file minus the first entry after the "----------" I am not even sure how to go about starting that, don't need help with a full script just how to go about doing part of the code. I know I need to unlink each file but how to I tell perl to remove each file after "--------" and first file path. File contents shown below should help clear things up. Thanks for the help in advance. Please let me know if you guys need any more details.

Code:
--------------------
/records/akr/00012.txt
/records/akz/001223.txt
/records/zzkr010112.txt
/records/updated/akz/001223.txt
---------------------
/records/xvf/3k33.txt
/records/arc/2231.txt
/records/updated/xvf/2k33.txt


So what I would like to do is unlink all files after the first entry which is preceded by ----------------
Script should unlink the following files

From the first section the following files should be unlinked 
/records/akz/001223.txt
/records/zzkr010112.txt
/records/updated/akz/001223.txt

From the second section the following files should be unlinked 
/records/arc/2231.txt
/records/updated/xvf/2k33.txt
 
This is potential to error, but how about
Code:
while ($line = <FH>) {
   if ($line =~ /^\-+$/) {
      $line = <FH> ;
      print $line
   }
};;;





* - Sorry Kevin, but I had to do that at least once :eek:
 
Don't forget to chomp it to get rid of the trailing newline if you are going to use it as an argument to unlink.
Perl:
use strict;
use warnings;

while (<DATA>) {
  print unless (/^-+$/ .. /\w/);
}  

__DATA__
--------------------
/records/akr/00012.txt
/records/akz/001223.txt
/records/zzkr010112.txt
/records/updated/akz/001223.txt
---------------------
/records/xvf/3k33.txt
/records/arc/2231.txt
/records/updated/xvf/2k33.txt
Also, Mr Brain, I feel compelled to draw your attention to those seemingly redundant semicolons outside the block in your previous posting... [smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hey guys thanks for the quick replies to both of you. Steve I tried the code you posted which worked perfectly reading from a file.

thanks again for the help really appreciate it, one thing I think I seem to do is over think when coding. I was thinking/making this out to be more complicated than it really ended up being.

 
* - Sorry Kevin, but I had to do that at least once :eek:

hehehe.... [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
One of the reasons I periodically haunt forums such as this is because I'll otherwise end up living in my own concept of what coding is and I loose sight of a much wider perspective (insert semi-colon here).

With that said, Steve, if you're still watching this thread, may I ask you to expound on
Code:
while (<DATA>) {
  print unless (/^-+$/ .. /\w/);
}
I get the gist of what is going on, but I'm kinda grasping at concepts. My only usage of (x .. y) has been in the form of taking splices out of an array (eg. @arr[3 .. 4]) I've (lightly) scanned through the Perl Cookbook and am not seeing anything similar. May I request a few lines of explanation of what's happing behind the scenes with respect to the print statement?
 
When it's used in a conditional, the two act like a flip-flop. In this case, it goes true when the first regex matches a line containing only hyphens. It stays that way until the second regex matches any word character, regardless of how many records have been read in the meantime. It's quite handy when you are reading files that have multiple lines of data for each logical entity.

There is also a ... version which doesn't allow the flip and the flop to happen on the same evaluation. See perlop for the full gory details.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
   [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^-+$[/purple][red]/[/red][red])[/red] [red]{[/red]
      <DATA>[red];[/red]
      [olive][b]next[/b][/olive][red];[/red]
   [red]}[/red]
   [url=http://perldoc.perl.org/functions/unlink.html][black][b]unlink[/b][/black][/url] [red]([/red][blue]$_[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/warn.html][black][b]warn[/b][/black][/url] [blue]$![/blue][red];[/red]
[red]}[/red]	
[teal]__DATA__[/teal][teal]	[/teal]
[teal]--------------------[/teal]
[teal]/records/akr/00012.txt[/teal]
[teal]/records/akz/001223.txt[/teal]
[teal]/records/zzkr010112.txt[/teal]
[teal]/records/updated/akz/001223.txt[/teal]
[teal]---------------------[/teal]
[teal]/records/xvf/3k33.txt[/teal]
[teal]/records/arc/2231.txt[/teal]
[teal]/records/updated/xvf/2k33.txt[/teal]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
oops, as Steve noted earlier, make sure to use chomp before using $_ in the unlink function.

Code:
chomp;
unlink ($_) or warn $!:

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top