awk script in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script in perl
# 1  
Old 11-08-2010
awk script in perl

Hi Linux users,
I have to convert a shell script in a perl script!

The command takes two files (two tables) and compares them to find the same values in 4 columns ($2" "$3" "$8" "$9) and prints out only the common lines.

This is the command:
cat first_file.txt | while read i; do cat second_file.txt | while read j; do primofile=`echo $i | awk '{ print $2" "$3" "$8" "$9 }'`; secondofile=`echo $j | awk '{ print $2" "$3" "$8" "$9 }'`; if [ "$primofile" == "$secondofile" ] ; then echo $primofile; fi ; done ; done

How can import this script in a Perl script?
Thank U very much
ME
# 2  
Old 11-08-2010
try this,

Code:
#!/usr/bin/perl

use strict;

my (@ar1,@ar2);
my ($file1_line,$file2_line);

open (FH1,"<","first_file.txt") || die "cannot open file1 for reading \n";
while (<FH1>) {
        chomp;
        @ar1=split(/\s+/,$_);
        $file1_line="$ar1[1] $ar1[2] $ar1[8] $ar1[9]";
        open (FH2,"<","second_file.txt") || die "cannot open file2 for reading \n";
        while (<FH2>) {
        chomp;
        @ar2=split(/\s+/,$_);
        $file2_line="$ar2[1] $ar2[2] $ar2[8] $ar2[9]";
        if ( "$file1_line" == "$file2_line" ) {
                print "$file1_line\n";
                }
        }
close(FH2);
}
close(FH1);

# 3  
Old 11-08-2010
Thanx Pravin27, It's work fine!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script in Perl or awk to remove multiple hyphens

Dear all, I have a database of compound words. I want to retain only strings with a single hyphen and identify those strings which have more than one hyphen. I am giving an example below test-test test-test-test test-test-test-test-test good-for-nothing The regex/script should remove all... (11 Replies)
Discussion started by: gimley
11 Replies

2. Shell Programming and Scripting

awk or perl script for preposition splitter

Hello, I am writing a Natural Language Parser and one of the tools I need is to separate prepositional phrase markers which begin with a Preposition. I have a long list of such markers (sample given below)and am looking for a script in awk or perl which will allow me to access a look-up file... (2 Replies)
Discussion started by: gimley
2 Replies

3. Shell Programming and Scripting

Multi platform script perl or awk

Hi gurus, I am trying to match records in following format: (-,username,domain1.co.uk)\ (-,username,domain2.co.uk) either awk or perl must be used. I am using cygwin. I wrote following code which works and matches both above entries: awk 'BEGIN {musr="(-,username,+.co.uk)"} {if... (8 Replies)
Discussion started by: wakatana
8 Replies

4. Shell Programming and Scripting

Help with convert awk script into perl

Input file (a list of input file name with *.txt extension): campus.com_icmp_ping_alive.txt data_local_cd_httpd.txt data_local_cd.txt new_local_cd_mysql.txt new_local_cd_nagios_content.txt Desired output file: data local_cd_httpd data local_cd new local_cd_mysql new ... (9 Replies)
Discussion started by: perl_beginner
9 Replies

5. Shell Programming and Scripting

usage of AWK command under perl script

i have two files as shown below t1.txt: argument1 argu2 argu37 t2.txt: 22 33 44 i want o/p as argument1 22 argu2 33 argu37 44 i am trying to merge two file under perl script using following system("paste t1.txt t2.txt | awk... (3 Replies)
Discussion started by: roopa
3 Replies

6. Shell Programming and Scripting

Shell script (not Perl) to parse xml with awk

Hi, I have to make an script according to these: - I have couples of files like: xxxxxxxxxxxxx.csv xxxxxxxxxxxxx_desc.xml - every xml file has diferent fields, but keeps this format: ........ <defaultName>2011-02-25T16:43:43.582Z</defaultName> ........... (2 Replies)
Discussion started by: Pluff
2 Replies

7. Shell Programming and Scripting

Executing AWK in a perl script using 'system'...

I have a simple perl script that looks similar to this: #!/usr/bin/perl/ # Have a lot of PERL code in the front of this script. #Would now like to execute a system command using AWK system (qq(cd /location && awk '/full/ {print $1;exit}' /myfile)); The system command in my perl script... (4 Replies)
Discussion started by: SysAdm2
4 Replies

8. Shell Programming and Scripting

Awk script into Perl

Hello, I have not programmed in Perl, but maybe someone can help me or point me to other links. I have searched for and found a solution to my initial problem. I have a text file of data where I want to search for a particular string but return the prior line. I found out here something that... (3 Replies)
Discussion started by: bsp18974
3 Replies

9. Shell Programming and Scripting

perl as awk replacement in a script.

Hey all, Im trying to write a script on windows, which Im not too familiar with. Im generally a bash scripting guy but am using perl for this case. My question is... I have this exact output: 2 Dir(s) 6,380,429,312 bytes free and I just need to get the number out... (4 Replies)
Discussion started by: trey85stang
4 Replies

10. Shell Programming and Scripting

embeding awk script in perl program

Collegues I have an AWK script like the following. { if ($2 ~ /JJ/ && $4 ~ /IN/) { print $2, $3, $4, $5 } } How can I embed it in a perl program. Jaganadh.G (5 Replies)
Discussion started by: jaganadh
5 Replies
Login or Register to Ask a Question