Perl one-liner convert to script format problem asking


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl one-liner convert to script format problem asking
# 1  
Old 12-16-2011
Perl one-liner convert to script format problem asking

Input_file_1:
Code:
ABC1   DEF11
ABC3   DEF7
ABC7   DEF36

Input_file_2:
Code:
DEF7   light   23
DEF11  over      2
DEF11  over    1
DEF17  blue       0

Perl one-liner that join two input file based on columns sharing a value (In this example, column 2 in Input_file_1 and column 1 in Input_file_2 sharing a value):
Code:
[home@perl_beginner]perl -e ' $col1=1; $col2=0; ($f1,$f2)=@ARGV; open(F2,$f2); while (<F2>) { s/\r?\n//; @F=split /\t/, $_; $line2{$F[$col2]} .= "$_\n" }; $count2 = $.; open(F1,$f1); while (<F1>) { s/\r?\n//; @F=split /\t/, $_; $x = $line2{$F[$col1]}; if ($x) { $num_changes = ($x =~ s/^/$_\t/gm); print $x; $merged += $num_changes } } ' Input_file_1 Input_file_2 > merge.txt
ABC1   DEF11   DEF11   over      2
ABC1   DEF11   DEF11   over    1
ABC3   DEF7    DEF7    light   23

Based on the above perl one-liner, do anybody got idea to change the above perl one liner into a perl shell script which allow us to type in any two input file and any column interest for merging when running the perl command?

Ideal running format:
Code:
[home@perl_beginner]perl merge.pl Input_file_1 1 Input_file_2 0 > merge.txt
ABC1   DEF11   DEF11   over      2
ABC1   DEF11   DEF11   over    1
ABC3   DEF7    DEF7    light   23

Thanks for any advice.

Last edited by perl_beginner; 12-16-2011 at 01:17 PM..
# 2  
Old 12-16-2011
Try (didn't test it, but it is not throwing syntax errors):
Code:
#!/usr/bin/perl
($f1,$col1,$f2,$col2)=@ARGV;
open(F2,$f2);
while (<F2>) {
  s/\r?\n//;
  @F=split /\t/, $_;
  $line2{$F[$col2]} .= "$_\n";
};
$count2 = $.; 
open(F1,$f1);
while (<F1>) {
  s/\r?\n//;
  @F=split /\t/, $_;
  $x = $line2{$F[$col1]};
  if ($x) {
    $num_changes = ($x =~ s/^/$_\t/gm);
    print $x; 
    $merged += $num_changes;
  }
}

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 12-17-2011
It don't specific output content.
# 4  
Old 12-17-2011
Thanks a lot, bartus11.
Your perl script work fine Smilie

Last edited by perl_beginner; 12-17-2011 at 03:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl one liner in bash script not replacing hours and minutes [HH:MM]

Hi I want to replace time stamp in the following line PROCNAME.Merge.exchMon.CODE.T_QSTART 08:45 read assuming the new time stamp is 09:45 ; the line is getting replaced as below :45 read I'm trying to use the perl one liner in bash script perl -pi... (4 Replies)
Discussion started by: charlie87
4 Replies

2. Shell Programming and Scripting

UNIX/PERL script to convert XML file to pipe delimited format

Hello, I need to get few values from a XML file and output needs to be written in another file with pipe delimited format. The Header & Footer of the Pipe Delimited file will be constant. The below is my sample XML file. I need to pull the values in between the XML tags <Operator_info to... (15 Replies)
Discussion started by: karthi1305561
15 Replies

3. Shell Programming and Scripting

perl module to convert xlsx format to xls format

Hi Folks, I have written a perl script that reads data from excel sheet(.xls) using Spreadsheet::ParseExcel module. But the problem is this module doesn't work for excel sheets with extension .xlsx. I have gone through Spreadsheet::XLSX module with which we can read from .xlsx file directly.... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Shell Programming and Scripting

perl one-liner to get yesterday's date in format dd-MMM-yy (i.e. 01-JAN-12)

I have the following perl one-liner to get yesterday's date, but I would like it in the form of dd-MMM-yy (for example: 01-JAN-12). Can someone alter the below code so I get the format I want? Also, could someone also give me a line for dd-Mmm-yy (for example 01-Jan-12)? Code: YEST=`perl -w... (3 Replies)
Discussion started by: thibodc
3 Replies

5. Shell Programming and Scripting

How can I do one liner import multiple custom .pm files in my perl script?

I am new for Perl I want to ask one question. I have around 50 custom packages which i am using in my Perl script. I want to import all .pm packages in my Perl script in an easy way. Right now i have to import each package individually. So Is there any way to do so?? Right Now i am doing like: ... (1 Reply)
Discussion started by: Navrattan Bansa
1 Replies

6. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

7. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

8. Shell Programming and Scripting

convert files into csv format using perl

Hi all perl gurus, I need your help to get the desired output in perl. I have a file which has text in it in the format Connection request start timestamp = 12/08/2008 00:58:36.956700 Connect request completion timestamp = 12/08/2008 00:58:36.959729 Application idle time ... (10 Replies)
Discussion started by: azs0309
10 Replies

9. Shell Programming and Scripting

Want To convert script in Linux format

I have scripts which I want to convert in Linux format. Note these scripts are in txt format.But I want to convert them in Linux, as DBA's will be using this script. Any command or utility which converts tht files in proper Linux format. Thanks in Adavce. Kunal (1 Reply)
Discussion started by: niceboykunal123
1 Replies

10. Shell Programming and Scripting

Need help passing variables in shell script to perl one-liner

I'm writing a script to automate some post-install tasks on RHEL4 servers. I need the following code to insert an 'A' in the middle of a string, then replace the string in a file. I know I can use sed to do this, but I'd like to use perl's in place edit so I don't have to write to a temp file,... (1 Reply)
Discussion started by: Xek
1 Replies
Login or Register to Ask a Question