Solved

Script to parse the log file

Posted on 2014-09-19
4
300 Views
Last Modified: 2014-11-24
I have a text file with below contents. Need a shell or perl script that will take path to the scripts_folder as parameter

text file contents
==========================================================================

Repository: https://someserver:9443/
Workspace: (1000) "test_scripts_ws"
  Component: (1001) "some_Automated_Scripts"
    Change sets:
      (1002) ---$ john "test memory" 17-Sep-2014 02:24 PM
        Changes:
          --a-- \Libs\path\to\some\file22.txt
          --m-- \Libs\file\some\folder\locg2.lcke
          --d-- \Libs\file\in\some\folder\lock.lck
          ---c- \Libs\file\path\to\test\folder\source.mtr
          ---c- \Libs\Action1\Resource.mtr
         
      (1003) ---$ john "Test exist:"  17-Sep-2014 12
:33 PM
        Changes:
          ---c- \Libs\path\to\some\folder\cript.mts
      (1004) ---$ john "Memory Fix #2" 17-Sep-2014 12:47 PM
        Changes:
          ---c- \Libs\path\tp\some\folder\test.txt

======================================================================================

The current folder structure is
                   
                     -->test
                       -->scripts_folder
                          -->Libs
                          -->Libs2
                          -->Libs3
                          -->test1  

  test.pl C:\test\scripts_folder\

1. Delete all the files in the scripts_folder directory except the one's in the above log file starting with  --c-  --a--   --m--

2. Ignore lines starting with --d--
 
3. Do not error out if the file matching above criteria is already deleted or doesnt exist in the scripts_folder  
    directory.
0
Comment
Question by:gaurav sharma
  • 2
  • 2
4 Comments
 

Author Comment

by:gaurav sharma
Comment Utility
For now I have :

cat required.txt | grep -e --a- -e --m-- -e ---c-
        --a-- \Libs\test1\test1\test1.txt
       --m-- \Libs\test1\test1\test11.txt
       ---c- \Libs\Libs\test2\test2\test22.txt
       ---c- \Libs\Libs\test2\test2\test2.txt
       ---c- \Libs\Libs\test3\test33.txt
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Is required.txt in the scripts_folder directory?
If not, how should the script find it?
If so, should it also be deleted?
0
 

Author Comment

by:gaurav sharma
Comment Utility
@Ozo: The required.txt is in the scripts_folder directory.
            This file should not be deleted.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
#!/usr/bin/perl
use strict;
use warnings;
require 5.014;
use File::Find;
my($path,%r);
die "Usage: $0 pathtothescripts_folder" unless $path=shift;
chdir $path or die "$path $!";
++$r{"/required.txt"};
open R,"<required.txt" or die "$path/required.txt $!";
while( <R> ){
    ++$r{$1=~s#[/\\]#/#gr} if /--(?:a-|m-|-c)-\s+(\S+)/;
}
close R;
find({wanted => sub{
    !-f or $r{s#(^\.)?[\\/]#/#gr} or unlink $_ or warn "$_ $!";
    },no_chdir=>1},".");
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now