The UNIX and Linux Forums  

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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script or piece of code where the data returned by a stored procedure you are writing enigma_83 Shell Programming and Scripting 3 04-25-2008 03:44 AM
Perl Unix Script Writing Dinkster UNIX for Dummies Questions & Answers 1 02-01-2008 12:09 PM
help for writing shell script to export table data sankarg Shell Programming and Scripting 3 10-18-2006 03:01 AM
help writing script file lanboy_2005 UNIX for Dummies Questions & Answers 19 03-10-2004 06:58 AM
Writing both 8-bit and 16-bit data to a file Breen High Level Programming 1 03-03-2004 09:59 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
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 -4 | tail -2 of file1 ...

now i need to add the first line and last lines to this tmpfile...

any suggestions are appreciated......thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: May 2006
Posts: 95
sounds like a job for sed:
hint:

sed -e '$d' = delete last line

sed -e '$!d' = delete everything except the last line
Reply With Quote
  #3 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 317
Quote:
Originally Posted by meghana View Post
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 -4 | tail -2 of file1 ...

now i need to add the first line and last lines to this tmpfile...

any suggestions are appreciated......thanks
I'm confused, why are you using perl if you are shelling out to the operating system? Unless file2 is gigantic, read it all into an array. Then use the array to do what you want.

Code:
open (FILE2, 'file2');
my @array = <FILE2>;
close FILE2;
my @first4 = @array[0..3];
my @last2 = @array[-2,-1];
my @rest_of_lines = @array[4..$#array-2];
Of course all those temp arrays are probably not necessary, I just use them to illustrate the array slice syntax you can use to achieve this using perl.

But I do not understand this requirement so I can't advise further:

Code:
now i need to add the first line and last lines to this tmpfile...
Can you clarify?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0