CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in

 



alogoc
New User

May 19, 2009, 3:32 AM

Post #1 of 11 (6975 views)
Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in Can't Post

  So the whole idea is about a program that will search inside the txt i will give whatever number starts with _ (Example : _10165,_09957 ),and with the results it will search the directory wich i give.Example C:\Id Boxes and tell me wich of the numbers that searched from the txt are not found in this folder as archives (other .txts).Then cut those that found and ask me to create a folder with them.

I have attached a copy of how exactly the txt looks.In this txt there are the numbers that the program will "see" and perfrom the opperation i said before.
Unless there is a program out there in the internet space wich already does it??

For further details please,question me!
Any help,comments,ideas would be much appreciated


(This post was edited by alogoc on May 19, 2009, 6:53 AM)
Attachments: IM1905091047_1.txt (0.32 KB)


vikas.deep
User

May 19, 2009, 6:24 AM

Post #2 of 11 (6963 views)
Re: [alogoc] Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in [In reply to] Can't Post

First of all
where have you attached
" I have attached a copy of how exactly the txt looks."
Whenever you are posting some thing to PerlGuru you are supposed to attach the attachments to your posts in this forum only I suspect that you have attached the textfile to some other post/ mail. I cannot see it here.

Next as far as I can understand you are looking to search for some text in a text file (and remember them; in may be some temporary array) and then selecting everything except the contents of that array in another file.

See that directory thing is nothing; while giving filenames just you have to give the path of your files if they are in a different directory that's all.

On the very basic you mean to search for something and then search for every thing except that thing

If you say Yes then we can proceed
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


alogoc
New User

May 19, 2009, 7:01 AM

Post #3 of 11 (6960 views)
Re: [vikas.deep] Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in [In reply to] Can't Post

Sorry!I didn't notice it wasn't uploaded!It's up now!

The txt as you will see is in lines,every line has a number that starts with _(5 numbers follow).That number is an archive (a .txt) in another directory.What i want,is to search in that directory and find those archives.Then move them to folder wich i will name.It's imporant to tell me wich archives are not found...


vikas.deep
User

May 19, 2009, 7:36 AM

Post #4 of 11 (6952 views)
Re: [alogoc] Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in [In reply to] Can't Post


Quote
an archive (a .txt) in another directory.What i want,is to search in that directory and find those archives.


It means you want to search for text files (name) in some other directory That is to say that a particular _(fivedigit) file is present in a particular directory or not

and then The files which are not matched should be moved to some
other place.

if you say yes then
you are not searching for some text in some files rather searching for existence of files
if so you can use -exist

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


alogoc
New User

May 19, 2009, 11:26 PM

Post #5 of 11 (6938 views)
Re: [vikas.deep] Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in [In reply to] Can't Post

First of all,thanks for answering my question.

Well on the file :C\Id Boxes image that there are like 10 thousand files.Their names are like this : "IM1905091047_1_03521.txt"
And yes an output folder were the .txts will be moved.It would be nice if the program would create a folder,so i type the name,and move them in.
As you notice the whole scene is about to organise those 10 thousand .txts and put them into folders.

About the search in the C:\Id Boxes now.The "IM1905091047" except the IM,is day,month,year,time and minutes wich the txt was created,and it's named that way.Here is the reason why the program much search with the "_" in front:

Let's say in this case we want to search for 1047,there is this .txt right here "IM1905091047_1_03521" and this txt right here "IM1905091047_1_10231".
The search will bring both the archives,and we do not want that smiley

That's why there is the "_" there.The program will search for the _1047.Exaclty as it's reffered in the attached .txt i sent you.

By the way there is a great chance to be dublicated files there,so a report would be nice


(This post was edited by alogoc on May 19, 2009, 11:30 PM)


vikas.deep
User

May 21, 2009, 2:36 AM

Post #6 of 11 (6917 views)
Re: [alogoc] Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in [In reply to] Can't Post

I do not know how to create folders using perl script. I can help with searching in a text file but creating a separate folder and then moving all the files (matched or not) in that folder is beyond my knowledge.

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


KevinR
Veteran


May 21, 2009, 10:45 AM

Post #7 of 11 (6912 views)
Re: [vikas.deep] Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in [In reply to] Can't Post


In Reply To
I do not know how to create folders using perl script. I can help with searching in a text file but creating a separate folder and then moving all the files (matched or not) in that folder is beyond my knowledge.

Quote


mkdir() <--- makes a folder

File::Path <-- creating or deleting directory trees

File::Copy <-- moving or copying files to other folders
-------------------------------------------------


vikas.deep
User

May 21, 2009, 11:59 PM

Post #8 of 11 (6906 views)
Re: [KevinR] Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in [In reply to] Can't Post

Hey
These appear to be simple linux commands. At least now (If not for anything else but as a token of respect to your post) when you have come up with that I should and (I shall) spent sometime over it. May be tonight itself (even though I am not an insomniac Smile.)

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


KevinR
Veteran


May 22, 2009, 4:24 PM

Post #9 of 11 (6896 views)
Re: [vikas.deep] Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in [In reply to] Can't Post


In Reply To
Hey
These appear to be simple linux commands. At least now (If not for anything else but as a token of respect to your post) when you have come up with that I should and (I shall) spent sometime over it. May be tonight itself (even though I am not an insomniac Smile.)

Quote


A lot of perl is derived from Unix so there is a lot of similarity.
-------------------------------------------------


FishMonger
Veteran / Moderator

May 22, 2009, 5:04 PM

Post #10 of 11 (6893 views)
Re: [vikas.deep] Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in [In reply to] Can't Post

mkdir is on Windows as well as *nix, just like the cd command and a few others. Microsoft "borrowed" a number of things from the UNIX world.

Those modules are platform independent which is one of the main reasons to use them instead of system calls.


vikas.deep
User

May 26, 2009, 10:34 PM

Post #11 of 11 (6811 views)
Re: [FishMonger] Program that will search specific numbers from inside a txt,cut them,and ask me to put the name of the folder to put them in [In reply to] Can't Post

After the person who originally posted the problem lost interest in this thread and I admitted of my lack of knowledge; I thought the thread is dead but for two gentleman who rekindled fire over here.

I went back to my reference manual and realized that there are not one or two but at least a dozen modules for all sort of file operations

I have selected two of them (one of them is same as suggested by Kevin, who had in fact suggested two modules.)

The problem is not solved. I have subdivided it into three problems

(1) There is a text file with complete names of files and the user only enters dates/ incomplete names to search for those files. The script has to search for all the files having that date/ incomplete name. This part is done.

(2)The script has to search in a directory all the files that matched user entered dates. This is where problem arises

(3) Move the files(matched or unmatched whatever desired) to a new folder. This can also be done

Quote
This is the sample of original text file
092209983 0922910165_10165
1
092209983 0922909983_09983
1
092209983 0922909983_09983
1
092209983 0922909957_09957


I have created three text files in the same folder as the script using the names as follows

Quote
0922909957_09957.txt
0922909983_09983.txt
0922910165_10165.txt


####The script follows####

Code
#! /usr/local/bin/perl -w 
use strict;
use warnings;
use File::CheckTree;
use File::Copy;
my @arra;
mkdir ("newfold");
open(my$FH,"text.txt") or die "Cannot open the file text:$!";
my @arr = <$FH>;
chomp @arr;
close $FH;
print "Enter the number to search for";
my $num = <>;
chomp $num;
$num = "_". $num;
foreach (@arr)
{
if ($_ =~ /$num/g)
{
push @arra,$_
}
}
my @new;
my @new1;
chomp @arra;
foreach (@arra)
{
push @new, split /\s/;
foreach (@new)
{
if($_ =~ /_/g)
{
push @new1,$_;
}
}
}
print "@new";
print "\n","@new1";
foreach (@new1)
{
$_ = $_."\."."txt";
}
print "\n","@new1";
for(my $index =0;$index<scalar@new1; $index++)
{
if (validate q{/root/Documents/$new1[$index] -e or die "$!"} )
{print "SUCCESS!!";
move ("/root/Documents/$new1[$index]","/root/Documents/newfold");
}
else
{
print "FAILURE!!";
}
}


Whenever I try with 09983 I get

Quote
[root@localhost Documents]# perl kev.pl
Enter the number to search for09983
092209983 0922909983_09983 092209983 0922909983_09983
0922909983_09983 0922909983_09983
0922909983_09983.txt 0922909983_09983.txtNo such file or directory at (eval 3) line 1, <> line 1.

but the file is there just "IF THE COMPUTER STARES A BIT HARDER"

The .txt was added because in case the person is working on windows
then appropriate extension is required.

Kindly help me


Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives