Need to remove first 6 lines and last line in a array ---- perl scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to remove first 6 lines and last line in a array ---- perl scripting
# 1  
Old 05-07-2013
Linux Need to remove first 6 lines and last line in a array ---- perl scripting

Hi

I have stored a command output in an array like below

@a = `xyz`;

actually xyz comnad will give the output like this

tracker
date
xxxxxxx
xxxxxxx
---------------------
1 a
2 b
----------------------

i have stored the "xyz" output to an Array @a.

i need to remove first 5 lines and last line and the output should be

1 a
2 b

i am sticking here


#!/usr/bin/perl -w

use strict;
use warnings;

@a = `xyz`;

foreach $i (@a) {
*************
************
}

in the above loop how can we remove the first 5 lines and last line in array...

could you please guide me...
# 2  
Old 05-07-2013
Here are a few ways
Using a slice:
Code:
 
@a = @a[ 5 .. $#a ];

Using splice:
Code:
 
splice @a, 0, 5;

Using shift:
Code:
 
shift @a for 1..5;

Using grep:
Code:
 
my $cnt = 0;
@a = grep { ++$cnt > 5 } @a;

Using map:
Code:
 
my $cnt = 0;
@a = map { ++$cnt < 5 ? ( ) : $_ } @a;


Last edited by vidyadhar85; 05-07-2013 at 02:32 AM..
This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 05-07-2013
Thank you very much,

the above codes are removing first 5 lines.

could you please tell me how to remove last line also

i need to remove first five lines and last one line....

---------- Post updated at 12:59 AM ---------- Previous update was at 12:55 AM ----------

Thank you friend, I got it

i have done like this "@a = @a[ 5 .. $#a-1 ];

its working fine

Thank you friend
# 4  
Old 05-07-2013
I guess pop fucntion would remove the last element..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Delete lines in an array Using perl

im having an array @check which contains text ..i want to open the array and i have to delete lines starting from a word called "check1" till "check2" for eg:- check1 Use descriptive titles when posting. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt".... (0 Replies)
Discussion started by: rajkrishna89
0 Replies

3. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

4. Shell Programming and Scripting

perl/shell need help to remove duplicate lines from files

Dear All, I have multiple files having number of records, consist of more than 10 columns some column values are duplicate and i want to remove these duplicate values from these files. Duplicate values may come in different files.... all files laying in single directory.. Need help to... (3 Replies)
Discussion started by: arvindng
3 Replies

5. Shell Programming and Scripting

Perl Scripting to remove single cotes (')

Hii, I am creating a perl script to read a text and remove the ' at the end of each line only. But I am not getting any sucess. The text file is as follows. exec ''sp_prepexec' exec ''sp_prepexec' exec ''sp_prepexec' exec ''sp_prepexec' exec ''sp_prepexec' Request your help to solve... (1 Reply)
Discussion started by: sagarbsa
1 Replies

6. UNIX for Advanced & Expert Users

Remove lines using perl

Hi, Using the egrep ,i can exclude header and trailer records in file.I want to do in perl.Please help egrep -iv 'head|trail' filename Thanks in advance MR (6 Replies)
Discussion started by: mohan705
6 Replies

7. Shell Programming and Scripting

How to remove the specific lines from file using perl

Can anyone tell me what could be the solution to following : I have one .txt file which contains some "seed" information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed. Please provide the script code... (4 Replies)
Discussion started by: dipakg
4 Replies

8. Shell Programming and Scripting

Remove an element from an array in PERL

Hello guys, I have the following question. Consider the following code in PERL for($xeAnumber=0; $xeAnumber<@xeAnumber; $xeAnumber++) { if(@xeAnumber==@final_file) { @final_file=@xeTimeStamp; }... (3 Replies)
Discussion started by: chriss_58
3 Replies

9. Shell Programming and Scripting

How to remove the lines from file using perl

Can anyone tell me what could be the solution to following : I have one .txt file which contains some seed information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed. here is the contents of .txt... (5 Replies)
Discussion started by: dipakg
5 Replies

10. Shell Programming and Scripting

Perl - New line in array elements

Hello, I have a comma delimited input feed file. The first field has directory location and the second field has file name. Ex of input feed: /export/appl/a,abc*.dat /export/appl/b,xyz*.dat /export/appl/c,pmn*.dat Under each directory, there would be many files like... . . .... (4 Replies)
Discussion started by: bperl
4 Replies
Login or Register to Ask a Question