The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: How to convert xxx@dom.ain.com to domaincom Juha Shell Programming and Scripting 6 04-30-2008 08:09 AM
convert the below perl sript to shell script mail2sant Shell Programming and Scripting 1 04-04-2008 01:36 PM
awk to perl convert jaganadh Shell Programming and Scripting 2 12-11-2007 11:11 AM
Sample Unix script file to convert .xml to .csv srinivasaphani Shell Programming and Scripting 4 08-27-2007 10:04 AM
how to convert unix .ksh script to windows .batch script 2.5lt V8 Shell Programming and Scripting 1 11-28-2006 12:52 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-25-2008
gholdbhurg gholdbhurg is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 26
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 (permalink)  
Old 03-25-2008
matrixmadhan matrixmadhan is online now Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
What is your requirement ? Could you please state that ?

What have you tried so far ?
  #3 (permalink)  
Old 03-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 01:47 PM.. Reason: Merging the two loops; fix indexing error (duh)
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0