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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script for Calling a function and writing all its contents to a file
# 1  
Old 05-19-2013
Perl script for Calling a function and writing all its contents to a file

I have a function which does awk proceessing

Code:
sub mergeDescription {
        system (q@awk -F'~' '
        NR == FNR {
                A[$1] = $1
                B[$2] = $2
                C[$1,$2] = $0
                next
        }
        {
                n = split ( C[$1,$3], V, "~" )
                if ( A[$1] && B[$3] )
                        print $0, V[n]
                else
                        print $0,"UNDEFINED"
        } ' OFS="~" file1 file2@);
}

I want to write all the output generated by this function to a file. Please suggest.

Last edited by Scott; 05-19-2013 at 01:34 PM.. Reason: Please use code tags
# 2  
Old 05-19-2013
1. Please use code tags, mate. Your code looks garbled.
2. It's not a good practice to invoke awk in a system call from perl, when you can do all of that (and much more) from perl itself. So, what is that you want to achieve? (Please provide input files, logic, desired output)
# 3  
Old 05-19-2013
Hi,
I have 2 files of below format.

File1
Code:
AA~1~STEVE~3.1~4.1~5.1
AA~2~DANIEL~3.2~4.2~5.2
BB~3~STEVE~3.3~4.3~5.3
BB~4~TIM~3.4~4.4~5.4

File 2
Code:
AA~STEVE~AA STEVE WORKS at AUTO COMPANY
AA~DANIEL~AA DANIEL IS A ELECTRICIAN
BB~STEVE~BB STEVE IS A COOK

I want to match 1st and 3rd column values of file 1 against 1st and 2nd column values of file2. If both match conditions satisfy then i want to print 3rd column of file2 at the end of file1.

If not then print "UNDEFINED" at end of file 1

Below is the expected output which i want to print in file1
Code:
AA~1~STEVE~3.1~4.1~5.1~AA STEVE WORKS at AUTO COMPANY
AA~2~DANIEL~3.2~4.2~5.2~AA DANIEL IS A ELECTRICIAN
BB~3~STEVE~3.3~4.3~5.3~BB STEVE IS A COOK
BB~4~TIM~3.4~4.4~5.4~UNDEFINED JOB

I created a function to fetch the data but now i am not able to write its output to a file
Code:
sub mergeDescription {
        system (q@awk -F'~' '
        NR == FNR {
                A[$1] = $1
                B[$2] = $2
                C[$1,$2] = $0
                next
        }
        {
                n = split ( C[$1,$3], V, "~" )
                if ( A[$1] && B[$3] )
                        print $0, V[n]
                else
                        print $0,"UNDEFINED"
        } ' OFS="~" file1 file2@);
}

Please suggest

Last edited by Franklin52; 05-20-2013 at 09:12 AM.. Reason: Please use code tags
# 4  
Old 05-20-2013
Code:
sub mergeDescription {
    open F1, "< file1";
    open F2, "< file2";
    
    my (%hash);
    
    while (<F2>) {
        chomp;
        (/([^~]*~[^~]*)~(.*)/) && ($hash{$1} = $2);
    }
    
    while (<F1>) {
        chomp;
        my @fields = split /~/;
        
        if (exists($hash{"$fields[0]~$fields[2]"})) {
            print join ("~", @fields), "~", $hash{"$fields[0]~$fields[2]"}, "\n";
        }
        else {
            print join ("~", @fields), "~UNDEFINED JOB\n";
        }
    }
    
    close F2;
    close F1;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

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

3. Shell Programming and Scripting

Help needed in calling path file from Perl script.

Hi All, I need help in accessing a path file (path.xyz_pqr) not having a shell shebang and exporting paths in the following syntax, export DB_SCRIPT_PATH="/abc/aash/scripts/db_scripts/xyz_pqr" export SRC_FILES_DIR="/bcd/fdw/incoming/xyz_pqr" I need to use the path contained in the... (8 Replies)
Discussion started by: xtatic
8 Replies

4. Shell Programming and Scripting

PERL: Function calling using parameters

I have two variables. I need to call them using the same function my $dat1 ="abc"; my $dat2 ="def"; my $check; filecheck($dat1,$dat2); sub filecheck($) { $check = shift; print "first name is $check\n"; } print "this is fine\n"; sub filecheck($) { $check = shift; print "second... (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

6. Shell Programming and Scripting

calling a php file from perl script..

Hi am new to perl script .. i need some useful tutorials links to learn perl script.. and a sample script to call a php file from perl script.. thanks.. (3 Replies)
Discussion started by: senkerth
3 Replies

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

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

9. Shell Programming and Scripting

Question for calling oracle function from perl

Dear Sir/Madam, I use the following way to call the oracle stored procedure in a perl script, but I do not know how to call a oracle function by the following way ? # ARGV is the oracle stored procedure name with parameters $str = "sqlplus -s <<-eof \n" . "$db_login... (0 Replies)
Discussion started by: ili
0 Replies

10. Shell Programming and Scripting

help for a perl script - writing to a data file

Hi, Here is my problem.. i have 2 files (file1, file2).. i have wrote the last two lines and first 4 lines of "file2" into two different variables .. say.. my $firstrec = `head -4 $file2`; my $lastrec = `tail -2 $file2`; and i write the rest of the file2 to a tmpfile and cat it with head... (2 Replies)
Discussion started by: meghana
2 Replies
Login or Register to Ask a Question