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?
# 1  
Old 04-01-2013
How to grep a pattern in perl?

hello Everyone i am a newbie.

i have a file which contains the following


Code:
E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\winclockmtq.lib
E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\unifilesmtq.lib
E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\dlmmgrmtq.lib
E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\asyncoremtq.lib
E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\baselibmtq.lib
E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\i18nmtq.lib
E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\utfstrmtq.lib
E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\rtlcoremtq.lib
E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\btkzlibmtq.lib


1) grep all the ".lib" from the file.

2) make a new directory and copy all the libs into it.


please suggest...

Last edited by radoulov; 04-01-2013 at 04:16 PM..
# 2  
Old 04-01-2013
So, according to your requirement I have rephrased the above.
  1. A file which contains numerous lines will the input
  2. Program have find all the .lib file names that are present only in the lines which has check_geomtools.exe on them
  3. After having all the lib files selected, you have to create a new folder
  4. Copy all the .lib files to that folder

Correct???
# 3  
Old 04-01-2013
yes you are right..
# 4  
Old 04-01-2013
You could grep all the lines and store them in an array. Now pass that array to a function. Inside that function, create a directory of your choice. Then split the lines with <space> as separator. Then you can either move or rename.

For renaming, you will need the file name alone. You could split it with "\" as the separator.

Hope this helps. Smilie
# 5  
Old 04-01-2013
I have successfully completed upto the third step as per your understanding. Smilie

Actually i have come across a new issue i.e. after splitting the paths, value[1] has to be copied to the newly created directory.

However facing little problem in copying the files to a newly created directory.
# 6  
Old 04-01-2013
Can you show us your code??

we could help you correct it Smilie

Anyway try like this
Code:
use File::Copy; #add this at the beginning :)
if (-d <your directory>){
print "<your directory> exists\n";
}

my @old_location = split(/ /,$ur_matched_line);
my @file_name = split(/\\/,$ur_matched_line);
my $new_location = $ur_directory . $file_name[-1];
copy($old_location[-1], $new_location);

# 7  
Old 04-01-2013
I am not done yet with it.Smilie

please have a look.

Code:
#!/usr/bin/perl

use File::Copy;

$my_file = "makefile";
open(HANDLE,"<$my_file");


while(<HANDLE>)
{
    if(/check_geomtools.exe:/./\.lib$/)
    {
     @libs = split(/: /);
         mkdir "Scratch";
     copy($libs[1], Scratch);
    }
         
}

close HANDLE;


Last edited by radoulov; 04-01-2013 at 04:17 PM..
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