help for a perl script - writing to a data file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help for a perl script - writing to a data file
# 1  
Old 02-04-2008
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  
Old 02-04-2008
sounds like a job for sed:
hint:

sed -e '$d' = delete last line

sed -e '$!d' = delete everything except the last line
# 3  
Old 02-04-2008
Quote:
Originally Posted by meghana
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

Script writing for tables with binary data

Hi There, I'm trying to write a simple script that will email me when we have an application job in a certain status that needs human intervention. I've used this script for other tables and it works great. However, this one gives me the warning that there is binary data so it might not. ... (2 Replies)
Discussion started by: Colel2
2 Replies

3. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

4. Shell Programming and Scripting

Need help in writing perl script

Hi, I am new to perl. I am trying to write a small perl script for search and replace in a file : ======================================================== #!/usr/bin/perl my $searchStr = "register_inst\.write_t\("; my $replaceStr = "model\.fc_block\."; open(FILE,"temp.sv") ||... (2 Replies)
Discussion started by: chettyravi
2 Replies

5. Shell Programming and Scripting

Perl script for Calling a function and writing all its contents to a file

I have a function which does awk proceessing sub mergeDescription { system (q@awk -F'~' ' NR == FNR { A = $1 B = $2 C = $0 next } { n = split ( C, V, "~" ) if... (3 Replies)
Discussion started by: crypto87
3 Replies

6. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

7. Shell Programming and Scripting

Perl script to Archive the data file after the load

I have written a perl scripts which loads the data. Now i want to modify the script, to Archive the Input file after the successful load of data. Can anyone please share it and help me .... Thanks. (2 Replies)
Discussion started by: msrahman
2 Replies

8. Shell Programming and Scripting

Need help in writing a script to create a new text file with specific data from existing two files

Hi, I have two text files. Need to create a third text file extracting specific data from first two existing files.. Text File 1: Format contains: SQL*Loader: Release 10.2.0.1.0 - Production on Wed Aug 4 21:06:34 2010 some text ............so on...and somwhere text like: Record 1:... (1 Reply)
Discussion started by: shashi143ibm
1 Replies

9. Shell Programming and Scripting

Need help with writing a perl script

Hi all! I have to write a perl script that gets trashholds from a file and match them with an output of a command. The trashhold file looks like this: "pl-it_prod.GW.Sync.reply.*" "500" "-1" "" "" "pl-it_prod.A.*" "100" "-1" "" "" "application.log" ... (29 Replies)
Discussion started by: eliraza6
29 Replies

10. Shell Programming and Scripting

help for writing shell script to export table data

Hi All, I need to write a shell script(ksh) to take the tables backup in oracle(exporting tables data). The tables list is not static, and those are selecting through dynamic sql query. Can any body help how to write this shell script. Thanks, (3 Replies)
Discussion started by: sankarg
3 Replies
Login or Register to Ask a Question