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
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 06:44 AM
Perl Unix Script Writing Dinkster UNIX for Dummies Questions & Answers 1 02-01-2008 04:09 PM
help for writing shell script to export table data sankarg Shell Programming and Scripting 3 10-18-2006 06:01 AM
help writing script file lanboy_2005 UNIX for Dummies Questions & Answers 19 03-10-2004 10:58 AM
Writing both 8-bit and 16-bit data to a file Breen High Level Programming 1 03-03-2004 01:59 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-04-2008
meghana meghana is offline
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
  #2 (permalink)  
Old 02-04-2008
grumpf grumpf is offline
Registered User
  
 

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

sed -e '$d' = delete last line

sed -e '$!d' = delete everything except the last line
  #3 (permalink)  
Old 02-04-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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?
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 07:24 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