Sponsored Content
Top Forums Shell Programming and Scripting How to grep a pattern in perl? Post 302788489 by PikK45 on Tuesday 2nd of April 2013 12:37:25 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Directory::Scratch(3pm) 				User Contributed Perl Documentation				   Directory::Scratch(3pm)

NAME
Directory::Scratch - Easy-to-use self-cleaning scratch space. SYNOPSIS
When writing test suites for modules that operate on files, it's often inconvenient to correctly create a platform-independent temporary storage space, manipulate files inside it, then clean it up when the test exits. The inconvenience usually results in tests that don't work everwhere, or worse, no tests at all. This module aims to eliminate that problem by making it easy to do things right. Example: use Directory::Scratch; my $temp = Directory::Scratch->new(); my $dir = $temp->mkdir('foo/bar'); my @lines= qw(This is a file with lots of lines); my $file = $temp->touch('foo/bar/baz', @lines); my $fh = openfile($file); print {$fh} "Here is another line. "; close $fh; $temp->delete('foo/bar/baz'); undef $temp; # everything else is removed # Directory::Scratch objects stringify to base $temp->touch('foo'); ok(-e "$temp/foo"); # /tmp/xYz837/foo should exist EXPORT
The first argument to the module is optional, but if specified, it's interperted as the name of the OS whose file naming semantics you want to use with Directory::Scratch. For example, if you choose "Unix", then you can provide paths to Directory::Scratch in UNIX-form ('foo/bar/baz') on any platform. Unix is the default if you don't choose anything explicitly. If you want to use the local platform's flavor (not recommended), specify an empty import list: use Directory::Scratch ''; # use local path flavor Recognized platforms (from File::Spec): Mac UNIX Win32 VMS OS2 The names are case sensitive, since they simply specify which "File::Spec::" module to use when splitting the path. EXAMPLE use Directory::Scratch 'Win32'; my $tmp = Directory::Scratch->new(); $tmp->touch("foo\bar\baz"); # and so on METHODS
The file arguments to these methods are always relative to the temporary directory. If you specify "touch('/etc/passwd')", then a file called "/tmp/whatever/etc/passwd" will be created instead. This means that the program's PWD is ignored (for these methods), and that a leading "/" on the filename is meaningless (and will cause portability problems). Finally, whenever a filename or path is returned, it is a Path::Class object rather than a string containing the filename. Usually, this object will act just like the string, but to be extra-safe, call "$path->stringify" to ensure that you're really getting a string. (Some clever modules try to determine whether a variable is a filename or a filehandle; these modules usually guess wrong when confronted with a "Path::Class" object.) new Creates a new temporary directory (via File::Temp and its defaults). When the object returned by this method goes out of scope, the directory and its contents are removed. my $temp = Directory::Scratch->new; my $another = $temp->new(); # will be under $temp # some File::Temp arguments get passed through (may be less portable) my $temp = Directory::Scratch->new( DIR => '/var/tmp', # be specific about where your files go CLEANUP => 0, # turn off automatic cleanup TEMPLATE => 'ScratchDirXXXX', # specify a template for the dirname ); If "DIR", "CLEANUP", or "TEMPLATE" are omitted, reasonable defaults are selected. "CLEANUP" is on by default, and "DIR" is set to "File::Spec-"tmpdir>; child Creates a new "Directory::Scratch" directory inside the current "base", copying TEMPLATE and CLEANUP options from the current instance. Returns a "Directory::Scratch" object. base Returns the full path of the temporary directory, as a Path::Class object. platform([$platform]) Returns the name of the platform that the filenames are being interperted as (i.e., "Win32" means that this module expects paths like "fooar", whereas "UNIX" means it expects "/foo/bar"). If $platform is sepcified, the platform is changed to the passed value. (Overrides the platform specified at module "use" time, for this instance only, not every "Directory::Scratch" object.) touch($filename, [@lines]) Creates a file named $filename, optionally containing the elements of @lines separated by the output record separator "$". The Path::Class object representing the new file is returned if the operation is successful, an exception is thrown otherwise. create_tree(%tree) Creates a file for every key/value pair if the hash, using the key as the filename and the value as the contents. If the value is an arrayref, the array is used as the optional @lines argument to "touch". If the value is a reference to "undef", then a directory is created instead of a file. Example: %tree = ( 'foo' => 'this is foo', 'bar/baz' => 'this is baz inside bar', 'lines' => [qw|this file contains 5 lines|], 'dir' => undef, ); $tmp->create_tree(%tree); In this case, two directories are created, "dir" and "bar"; and three files are created, "foo", "baz" (inside "bar"), and "lines". "foo" and "baz" contain a single line, while "lines" contains 5 lines. openfile($filename) Opens $filename for writing and reading ("+>"), and returns the filehandle. If $filename already exists, it will be truncated. It's up to you to take care of flushing/closing. In list context, returns both the filehandle and the filename "($fh, $path)". mkdir($directory) Creates a directory (and its parents, if necessary) inside the temporary directory and returns its name. Any leading "/" on the directory name is ignored; all directories are created inside the "base". The full path of this directory is returned if the operation is successful, otherwise an exception is thrown. tempfile([$path]) Returns an empty filehandle + filename in $path. If $path is omitted, the base directory is assumed. See File::Temp::tempfile. my($fh,$name) = $scratch->tempfile; exists($file) Returns the file's real (system) path if $file exists, undefined otherwise. Example: my $path = $tmp->exists($file); if(defined $path){ say "Looks like you have a file at $path!"; open(my $fh, '>>', $path) or die $!; print {$fh} "add another line "; close $fh or die $!; } else { say "No file called $file." } stat($file) Stats $file. In list context, returns the list returned by the "stat" builtin. In scalar context, returns a "File::stat" object. read($file) Returns the contents of $file. In array context, returns a list of chompped lines. In scalar context, returns the raw octets of the file (with any trailing newline removed). If you wrote the file with $, set, you'll want to set $/ to $, when reading the file back in: local $, = '!'; $tmp->touch('foo', qw{foo bar baz}); # writes "foo!bar!baz!" to disk scalar $tmp->read('foo') # returns "foo!bar!baz!" $tmp->read('foo') # returns ("foo!bar!baz!") local $/ = '!'; $tmp->read('foo') # returns ("foo", "bar", "baz") write($file, @lines) Replaces the contents of file with @lines. Each line will be ended with a " ", or $, if it is defined. The file will be created if necessary. append($file, @lines) Appends @lines to $file, as per "write". randfile() Generates a file with random string data in it. If String::Random is available, it will be used to generate the file's data. Takes 0, 1, or 2 arguments - default size, max size, or size range. A max size of 0 will cause an exception to be thrown. Examples: my $file = $temp->randfile(); # size is between 1024 and 131072 my $file = $temp->randfile( 4192 ); # size is below 4129 my $file = $temp->randfile( 1000000, 4000000 ); link($from, $to) Symlinks a file in the temporary directory to another file in the temporary directory. Note: symlinks are not supported on Win32. Portable code must not use this method. (The method will "croak" if it won't work.) ls([$path]) Returns a list (in no particular order) of all files below $path. If $path is omitted, the root is assumed. Note that directories are not returned. If $path does not exist, an exception is thrown. delete($path) Deletes the named file or directory at $path. If the path is removed successfully, the method returns true. Otherwise, an exception is thrown. (Note: delete means "unlink" for a file and "rmdir" for a directory. "delete"-ing an unempty directory is an error.) chmod($octal_permissions, @files) Sets the permissions $octal_permissions on @files, returning the number of files successfully changed. Note that '0644' is "--w----r-T", not "-rw-r--r--". You need to pass in "oct('0644')" or a literal 0644 for this method to DWIM. The method is just a passthru to perl's built-in "chmod" function, so see "perldoc -f chmod" for full details. cleanup Forces an immediate cleanup of the current object's directory. See File::Path's rmtree(). It is not safe to use the object after this method is called. ENVIRONMENT
If the "PERL_DIRECTORYSCRATCH_CLEANUP" variable is set to 0, automatic cleanup will be suppressed. PATCHES
Commentary, patches, etc. are most welcome. If you send a patch, try patching the git version available from: <git://git.jrock.us/Directory-Scratch>. You can check out a copy by running: git clone git://git.jrock.us/Directory-Scratch Then you can use git to commit changes and then e-mail me a patch, or you can publish the repository and ask me to pull the changes. More information about git is available from <http://git.or.cz/> SEE ALSO
L<File::Temp> L<File::Path> L<File::Spec> L<Path::Class> BUGS
Please report any bugs or feature through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Directory-Scratch>. ACKNOWLEDGEMENTS
Thanks to Al Tobey (TOBEYA) for some excellent patches, notably: "child" Random Files ("randfile") "tempfile" "openfile" COPYRIGHT &; LICENSE Copyright 2006 Jonathan Rockway, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.0 2008-06-09 Directory::Scratch(3pm)
All times are GMT -4. The time now is 10:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy