convert unix script to perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting convert unix script to perl
# 1  
Old 03-25-2008
convert unix script to perl

Hi,
I have these lines in a unix script:

FILEONE = /<filepath1>/<filename1.txt>
FILENEW = /<filepath2>/<filename2.txt>
head -5 $FILEONE | sed '1d' > $FILENEW
PARAM1 = `cat $FILENEW | awk '{print $2;}' `
echo "Param1 Value: $PARAM1"

What's the correct syntax of the above lines if same logic is used on perl?

my $FILEONE = "/<filepath1>/<filename1.txt>";
my $FILENEW = "/<filepath2>/<filename2.txt>";
`head -5 $FILEONE | sed '1d' > $FILENEW`;
my $PARAM1 = `cat \$FILENEW | awk '\{print $2;\}' `;
print ("Param1 Value:" . $PARAM1 ."\n");

Please advise if correct or how to better.
Thanks
# 2  
Old 03-25-2008
What is your requirement ? Could you please state that ?

What have you tried so far ?
# 3  
Old 03-25-2008
Quote:
Originally Posted by gholdbhurg;302178443[CODE
FILEONE = /<filepath1>/<filename1.txt>
FILENEW = /<filepath2>/<filename2.txt>
head -5 $FILEONE | sed '1d' > $FILENEW[/CODE]
Print four lines starting from line 2?

Code:
my $file1 = "/path/to/file1";
my $file2 = "/path/to/file2";

open (R, $file1) || die "Could not open $file1: $!\n";
open (W, ">$file2") || die "Could not open $file2: $!\n";
while (<R>) {
  print W unless $. == 1;
  last if $. == 5;
}
close R;
close W;

This can probably be abbreviated but if you are building a larger script, you will probably need roughly these pieces.

Quote:
Code:
PARAM1 = `cat $FILENEW | awk '{print $2;}' `
echo "Param1 Value: $PARAM1"

Create a space-separated value based on the second field of the lines in a file? This is not quite as natural in Perl but also not hard to do. I sort of fail to see any common use case for this, though.

Code:
my @g;
while (<R>) {
  my @f = split;
  push @g, $f[1];  # array indices are zero-based, so this is the second field
}
print(join (" ", @g), "\n");

Notice the use of an array to collect the values, and then a simple transform on it when you're done.

If you want to merge this with the first loop over file1, you can do something like

Code:
  print W if (2..5);
  # take out the "last"
  my @f = split;
  push @g, $f[1];


Last edited by era; 03-25-2008 at 02:47 PM.. Reason: Merging the two loops; fix indexing error (duh)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can someone convert this python script to perl

There is a python script that I would like converted to a perl script. If someone has the time to convert the script I would appreciate it. You can find the script below: reboot-mb8600/reboot-mb8600.py at master . j4m3z0r/reboot-mb8600 . GitHub #!/usr/bin/python ''' A hacky script to... (1 Reply)
Discussion started by: azdps
1 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

UNIX or Perl script to convert JSON to CSV

Is there a Unix or Perl script that converts JSON files to CSV or tab delimited format? We are running AIX 6.1. Thanks in advance! (1 Reply)
Discussion started by: warpmail
1 Replies

4. Shell Programming and Scripting

Perl Script remove digits and convert

Hello Guy's Quick question which im sure many can answer in seconds. Basically I have a perl script which is running commands to an element and then taking some of the the output and printing it to the screen. One of the outputs is a variable Hex Number. What I would like to do is strip... (1 Reply)
Discussion started by: mutley2202
1 Replies

5. Shell Programming and Scripting

Convert UNIX to perl

Hi, Can someone convert the code into perl ? x=(a b c) y=(d e) times=$((${#x} * ${#y})) ((xi=yi=0)) for((i=1;i<=times;i++,xi++,yi++)) do if((xi>${#x}-1));then xi=0;fi if((yi>${#y}-1));then yi=0;fi print ${x},${y} done (4 Replies)
Discussion started by: giri_luck
4 Replies

6. Shell Programming and Scripting

Convert shell script to Perl

Hello,,I have a very small script that contains these lines; and it works perfectly; however I need to use Perl now as I will need to feel variables from a MySQL table into this; to it would be nice to start by converting this first... find / -perm 777 \( -type f -o -type d \) -exec ls -lid {}... (1 Reply)
Discussion started by: gvolpini
1 Replies

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

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

9. Shell Programming and Scripting

Convert .sh script into perl

Good afternoon to you all I really need your help I have the following script developed in .sh and I need to convert it into perl. Can someone help me do it please? Here´s the script: ############################################## ############################################## ... (3 Replies)
Discussion started by: zarahel
3 Replies

10. Shell Programming and Scripting

Need to convert ksh script to Perl

Guys I am new to this forum, this may seem like a cheeky request. I have been asked by my manager to convert this ksh script to Perl. I do not have the foggiest about Perl and would appreciate any help on this. Basically this scipt automates a recovery process for EMC Legato Networker. It will... (1 Reply)
Discussion started by: rahimm1
1 Replies
Login or Register to Ask a Question