Sponsored Content
Homework and Emergencies Homework & Coursework Questions Shell script calling Perl function, sort and find data, write to new files Post 302567752 by MacMonster on Monday 24th of October 2011 11:16:19 PM
Old 10-25-2011
Quote:
Originally Posted by kowit010
Any idea how to get it to display the number line it's on? I thought of adding a counting integer variable to the code which would increment "++" if it finds a match, but not sure if that would work right? I also would like to swap the case switch for an if loop so I can keep the shell sort function in the same shell (and not have to call a second shell), unless you know how I can put multiple lines of code in a case switch (not just one thing; I've tried to put multiple lines in a case but cannot get that to work right)?
You already put the content lines of the file into an array, so you can use the array index to display the line number. Using an extra counter is not necessary. Please see the comments in the code below.

Code:
#!/usr/bin/perl -w

# Why need this line?
#use 5.010;

my $look = shift;

print "Enter a word to search for\n";
my $find = <STDIN>;
chomp($find); # Remove the newline at the end. Otherwise, pattern matching will fail

print "Lines that matched $find\n";
open(FILE, "<$look");
my @file_array = <FILE>;
my $found = 0; # Use a flag to indicate the word can be matched or not

# Use for loop and (i + 1) will be the line number
for (my $i = 0; $i < @file_array; $i++)
{
    my $line = $file_array[$i];
    chomp($line); # Remove the newline at the end before matching

    if ($line =~ m/$find/i)
    {
        print "The word $find is found on line ".($i + 1).": $line\n"; # Display the matched line with (i + 1) as line number
        $found = 1;
    }
}

# Put this out of the loop since it should display only once
print "Not found\n" if (!$found);

close FILE;


Last edited by MacMonster; 10-25-2011 at 12:18 AM.. Reason: Add some comments
This User Gave Thanks to MacMonster For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

calling one function from another shell script

i have a function defined in one ksh (ksh 1) i want to use that function in another ksh (ksh 2) i am using . $<directoryname>/<ksh name> i am calling the function defined in ksh 1 in ksh 2 i want the returnstatus from the above operation but it is not executing the function what i... (1 Reply)
Discussion started by: trichyselva
1 Replies

2. Shell Programming and Scripting

Calling a C-function froma shell script

Hi, I have searched the forum for the query, But i didnt find an exact answer. I have a script(1.sh) and a c program(sample.c) sample.c contains many function definitions.( run(), find(), add() etc). I want to call functions in sample.c from 1.sh and use the return value in 1.sh... (3 Replies)
Discussion started by: jisha
3 Replies

3. Shell Programming and Scripting

Calling a C-function froma Perl script

Hi All, How can we call a c function from a perl script? Is it the same way as we do for shell script ? Thanks in advance JS (9 Replies)
Discussion started by: jisha
9 Replies

4. Shell Programming and Scripting

calling a function in Shell script troubleshooting

Some Code After Some code part is executed the control doesnt go to rvin_doxx_scrt.. and the script exits rvin_doxx_scrt() { Some Code } if (som code) ... (4 Replies)
Discussion started by: ultimatix
4 Replies

5. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

6. Shell Programming and Scripting

Perl script to load data by calling sqlldr

Hello all, I know this is Unix forum, but i also know that there are some experts here who can help me out with this situation; I am loading a data file into oracle table using Perl script by calling sqlldr script. It does not do anything, and no data is getting loaded. Any help,... (2 Replies)
Discussion started by: msrahman
2 Replies

7. Shell Programming and Scripting

Calling a function in cpp file inside shell script

Hi I need to call a function written in a cpp file with arguments inside the shell script..Can anyone help me how to do this:( (1 Reply)
Discussion started by: rkrish
1 Replies

8. Shell Programming and Scripting

Calling Pl/sql function in shell script to modify csv

I need to 1.Open a csv 2.Process the csv i.e. Modify 2 column in the csv. To modify the column the value needs to be passed to a pl/sql function and the return value should be updated For eg: If column 2 E,then E will be passed in database function which will return Employee. 3. Write a... (5 Replies)
Discussion started by: Chinky23
5 Replies

9. Shell Programming and Scripting

Perl script for Calling a function and writing all its contents to a file

I have a function which does awk proceessing sub mergeDescription { system (q@awk -F'~' ' NR == FNR { A = $1 B = $2 C = $0 next } { n = split ( C, V, "~" ) if... (3 Replies)
Discussion started by: crypto87
3 Replies

10. Shell Programming and Scripting

Need help to write a function in shell scripting to execute sql files

Hi, I am new to shell scripting and i need to write a automation script to execute sql files. I need to check the table if it is there in system tables and need to write a function to call the .sql files. For ex. I have a.sql,b.sql,c.sql files, where the sql file contains DELETE and INSERT... (1 Reply)
Discussion started by: Samah
1 Replies
GREP(1) 						      General Commands Manual							   GREP(1)

NAME
grep - search a file for a pattern SYNOPSIS
grep [ option ... ] pattern [ file ... ] DESCRIPTION
Grep searches the input files (standard input default) for lines (with newlines excluded) that match the pattern, a regular expression as defined in regexp(6). Normally, each line matching the pattern is `selected', and each selected line is copied to the standard output. The options are -c Print only a count of matching lines. -h Do not print file name tags (headers) with output lines. -i Ignore alphabetic case distinctions. The implementation folds into lower case all letters in the pattern and input before interpre- tation. Matched lines are printed in their original form. -l (ell) Print the names of files with selected lines; don't print the lines. -L Print the names of files with no selected lines; the converse of -l. -n Mark each printed line with its line number counted in its file. -s Produce no output, but return status. -v Reverse: print lines that do not match the pattern. Output lines are tagged by file name when there is more than one input file. (To force this tagging, include /dev/null as a file name argument.) Care should be taken when using the shell metacharacters $*[^|()= and newline in pattern; it is safest to enclose the entire expression in single quotes '...'. SOURCE
/sys/src/cmd/grep.c SEE ALSO
ed(1), awk(1), sed(1), sam(1), regexp(6) DIAGNOSTICS
Exit status is null if any lines are selected, or non-null when no lines are selected or an error occurs. GREP(1)
All times are GMT -4. The time now is 03:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy