How to grep a pattern in perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep a pattern in perl?
# 8  
Old 04-02-2013
here in your code.. You are trying to create "Scratch" directory for every line matching.. That's not correct. Do one thing.

Create an array for storing all file names. After that create the directory (use absolute path). Then for each file name in that array, copy the file name to to scratch.

I wonder that copy($libs[1], Scratch); would work.

Apart from this, your code should do what you want Smilie
# 9  
Old 04-02-2013
i am unable to copy the files could you please help me out with the code..

Last edited by Rashid Khan; 04-02-2013 at 03:35 AM..
# 10  
Old 04-02-2013
Code:
#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;

my @files;
my @file_name;
my $folder = "./scratch" #please use absolute path
my $my_file = "makefile"; #please use absolute path
open(HANDLE,"<$my_file");

while(<HANDLE>)
{
    if(/check_geomtools.exe:/./\.lib$/)
    {
     @libs = split(/: /);
     push (@files, $libs[1]);
     }
}
     if ( -d $folder ) {
     mkdir ($folder, 0700);
     print "Created $folder\n";
     }
     
     foreach my $file (@files){
          @filename = split(/\\/,$file);
          copy($file,$folder.$filename[-1]);
    }
         
}

close HANDLE;

Hope this should work!

Last edited by PikK45; 04-02-2013 at 08:22 AM..
# 11  
Old 04-02-2013
still the .libs are not getting copied in the "$folder".

"@filename = split(/\\/,$file);"

we don't need to split "$file" beacuse that is not required.
# 12  
Old 04-02-2013
Quote:
Originally Posted by Rashid Khan
still the .libs are not getting copied in the "$folder".

@filename = split(/\\/,$file);

we don't need to split "$file" beacuse that is not required.
The code above is used to split the file name alone from the absolute path which was in $file. Syntax of copy is copy (file1 ,file2).

So I coded so that it will be executed like
Code:
copy(absolute_name<$file>, new_name<$folder.$filename[-1]>)

Guess it's not working.

Try
Code:
@filename = split(/\\/,$file);
my new_file = $folder . $filename[-1];
copy($file,$new_file) or die "Copy Failed";

# 13  
Old 04-03-2013
bingoo!!!!

Successfully copied the files with some modifications in the code.

I want to zip the folder($folder) and delete "scratch" once scratch.zip is created.

Here is the code


Code:
#!/usr/bin/perl

use File::Copy qw(copy);
use Archive::Zip qw/ :ERROR_CODES :CONSTANTS/;


my @files;
my $folder = "scratch"; 
my $my_file = "makefile"; 
open(HANDLE,"<$my_file");

while(<HANDLE>)
{
    if(/check_geomtools.exe:/./\.lib$/)
    {
     @libs = split(/: /);
     push (@files, $libs[1]);
     }
}
close HANDLE;
     if ( -d $folder )
	 {	
		print"$folder already exists..\ndeleting..\n";
		rmdir "$folder";
		print"$folder deleted..\ncreating new $folder\n";
		mkdir $folder;
		print "Created $folder\n";
	}
     
     foreach (@files)
	{
		chomp;
		copy($_,$folder);
    }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

2. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

3. Shell Programming and Scripting

How to Grep than scan line below grep pattern

Hello Colleagues, I have a file that looks like below. 6-12731913-12731913 9230760143480 410018547148230 20131002193434+0500 20131002193434+0500 ;20131002T161031000-10.50.241.21-21912131-1419034760, ver: 0 20131009 92220056296730 CC0P abc Core_Context_R1A SMS 6-12726796-12726796... (14 Replies)
Discussion started by: umarsatti
14 Replies

4. Shell Programming and Scripting

Grep pattern in PERL

Hi, Can anyone tell me how this grep works? Type of input is not known Thanks in advance (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. Shell Programming and Scripting

Want to grep for a pattern and display the content above that pattern

Hi, When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part. For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

7. Shell Programming and Scripting

pattern grep using Perl in .TSV file

Hi All, I have a .TSV extension file having ~1 Gig data and I need to grep a pattern in that file using perl. I am not able to read the file using perl any suggestions on this/ If I Change the format my data gets mismangled so I am bothered about using specific format as well. #!... (3 Replies)
Discussion started by: vmsenthil
3 Replies

8. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

9. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies

10. Shell Programming and Scripting

perl pattern matching vs. grep

I originally had a shell script that did a grep 10 times to pull out the number of times a certain pattern occured in a file: ie... aOccurances=`grep aPattern file|wc -l` bOccurances=`grep bPattern file|wc -l` ... ... ... fOccurances=`grep fPattern file|wc -l` As the file got bigger with... (0 Replies)
Discussion started by: junkmail426
0 Replies
Login or Register to Ask a Question