Sponsored Content
Top Forums Shell Programming and Scripting Need help in Perl Script for printing next line Post 302583351 by Skrynesaver on Tuesday 20th of December 2011 08:33:42 AM
Old 12-20-2011
Quote:
Originally Posted by rajkrishna89
Code:
my $in_file = 'check.doc';
open my $in_fh, '<', $in_file or die "Could not open file $in_file: $!
+";
my $out_file = 'output.txt';
open my $out_fh, '>', $out_file or die "Could not open file $out_file:
+ $!";
while ( my $line = <$in_fh> )
{
chomp ($_);
$line if ($_ =~ /^Customer ID: Yes/);
print "$out_fh}";
}
close $in_fh  or die "Could not close file $in_file: $!";
close $out_fh or die "Could not close file $out_file: $!";

Your error is due to the fact that you are reading the lines of input.txt into the $line variable, but you use the default variable $_ in the loop.

To add multiple files, simply loop through the file names, by wrapping the existing script with a for each construct, eg.
Code:
for my $filename (@ARGV){

To output the result in an Excel readable format you could either write it as a CSV file or if you are running Perl in a Windows environment you could use the Spreadsheet::WriteExcel module.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

printing an empty line in a file (perl)

I know this must be really easy, but i can't get it to work I've got a perl script, with a file. I want to print an empty line, and the following doesn't seem to work: print nameoffile "\n" thanks for your help!! (3 Replies)
Discussion started by: kfad
3 Replies

2. Shell Programming and Scripting

Perl script to search a line and copy it to another line

Hi I have a log file (say log.txt). I have to search for a line which has the string ( say ERROR) in the log file and copy 15 lines after this into another file (say error.txt). Can someone give me the code and this has to be in PERL Thanks in advance Ammu (3 Replies)
Discussion started by: ammu
3 Replies

3. UNIX for Dummies Questions & Answers

Perl searching and printing multiple target in the same line

Hello, I'm trying to create a program in perl called myfind.pl; To use the program: (at the command line)$ program.pl keyword filename note: the keyword is any word or regular expression and it should display the result just like when you 'cat' the file name but with the keyword in... (2 Replies)
Discussion started by: Horizon666
2 Replies

4. Shell Programming and Scripting

Perl, searching multiple files and printing returned line to new file

I am trying to find a way to utilise the full potential of my cpu cores and memory on my windows machine. Now, I am quite familiar with grep, however, running a Unix based OS is not an option right now. Unfortunately, the 32 bit grep for windows that I am running, I cannot run multiple... (1 Reply)
Discussion started by: Moloch
1 Replies

5. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

6. Shell Programming and Scripting

Printing the line number in bash script

Hi, I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened. Thanks, (6 Replies)
Discussion started by: suryaemlinux
6 Replies

7. Shell Programming and Scripting

Help with printing new line in shell script

I'm having a script which outputs four different attributes A, B, C, D for a list of users. I want to insert expression such that once the output is generated for first user, output for next user should be printed in new line. Please help. (4 Replies)
Discussion started by: surdileep
4 Replies

8. Homework & Coursework Questions

[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools. This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input. Sample input from <> operator: Hello World This is hello a sample... (2 Replies)
Discussion started by: D2K
2 Replies

9. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

10. Shell Programming and Scripting

Printing line in shell script

Need assistance in getting a shell program . I have csv file and each line has comma separated number. I wanted to take of the comas and print each number in each line . below example. Appreicate your help Row with number ... (14 Replies)
Discussion started by: ajayram_arya
14 Replies
NetSDS::Util::File(3pm) 				User Contributed Perl Documentation				   NetSDS::Util::File(3pm)

NAME
NetSDS::Util::File - file related utilities SYNOPSIS
use NetSDS::Util::File qw(file_read); my $passwd = file_read('/etc/passwd'); file_move('/etc/passwd', '/tmp/find_this'); DESCRIPTION
"NetSDS::Util::File" module contains some routines for files and directories processing tasks like creating, reading, writing, copying and moving files and catalogs. This module of cource uses such well known things like File::Spec, File::Path, File::Copy and others. EXPORTED FUNCTIONS
is_handle($var) - check if argument is a file handle Paramters: some variable Returns: 1 if it's file handle or undef otherwise if (is_handle($var)) { reset_handle($fh); } reset_handle($fh) - reset file handle Paramters: file handle Returns: nothing This function tries to set filehandle to begin of file and set binmode on it. my $fh = file_open('/etc/passwd'); ... do something with file ... reset_handle($fh); # We can read it from the beginning file_open($file) - open file Paramters: file name or file handle Returns: file handle This function provides unified API for opening files. my $f = file_open('/etc/passwd'); file_read($file) - read file to scalar Paramters: file name or file handle Returns: scalar content of file This function provides ability to read file content to scalar variable. my $data = file_read('/etc/passwd'); print "Passwords file: $data "; file_write($file, $data) - write scalar data to file Paramters: file name or open file handle Returns: length of written data or undef in case of error my $data = 'This should be file'; file_write('/tmp/file.dat', $data); file_copy($in_file, $out_file) - copy file Paramters: input file name, output file name Returns: This function copy file to new location. file_move($in_file, $out_file) - move file Paramters: input file name, output file name Returns: 1 or undef This function moves old file to new location. file_temp($dir) - create temporary file Creates new temp file and return its handle dir_create($dir) - create directory with parents Paramters: directory name Returns: directory name or undef # Will create all parent catalogs if necessary dir_create('/var/log/NetSDS/xxx'); dir_delete($dir) - remove directory recursive Paramters: directory name Returns: dir name or undef if error print "We need no libs!"; dir_delete('/usr/lib'); dir_read($dir, $ext) - read files list from catalog Paramters: directory name, extension of files to read Returns: list of files in catalog my @logs = @{ dir_read('/var/log/httpd', 'log') }; print "Logs are: " . join (', ', @logs); dir_read_recursive($dir, $ext, [$res]) - read all files list recursive Paramters: $start catalog, $extension Returns: list of files with extension from parameters my $tpls = dir_read_recursive('/etc/NetSDS', 'tmpl'); foreach my $tpl (@$tpls) { pritn "Template: $tpl "; } exec_external($prog, [$param1, ... $paramN]) - execute external program Paramters: pragram name, arguments list (see perldoc -f system) Returns: 1 if ok, undef otherwise This function calls system() with given parameters and returns 1 if everything happened correctly (program executed and returned correct result). if (exec_external('/bin/rm', '-rf', '/')) { print "Hey! We removed the world!"; } EXAMPLES
None yet BUGS
Unknown yet SEE ALSO
IO::Handle, IO::Scalar, IO::File, File::Spec, File::Copy, File::Path, system() TODO
1. Implement more detailed error handling AUTHOR
Valentyn Solomko <pere@pere.org.ua> Michael Bochkaryov <misha@rattler.kiev.ua> perl v5.12.4 2011-08-27 NetSDS::Util::File(3pm)
All times are GMT -4. The time now is 04:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy