Remove lines using perl


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Remove lines using perl
# 1  
Old 06-13-2008
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
# 2  
Old 06-18-2008
if you are reading a file in a perl script, and want to exclude some part of it (header and trailer) from processing, then there is no need to invoke an external program such as egrep. you can exclude whatever part you want to with the help of perl systax. something like:
Code:
while(<FH>) {
    next  if (m/head/);     # Skip header
    next  if (m/trail/);    # Skip trailer
    # process this line
}

or better yet,
Code:
next if (m/(head|trail)/);    # Skip header and trailer

# 3  
Old 06-18-2008
Hi,
Thanks for your reply.I want to do it in single command line


Thanks in advance
MR
# 4  
Old 06-18-2008
Code:
perl -ne 'print unless /head|tail/' file

If your requirement is to make a one-liner, what's wrong with the egrep solution you had already?
# 5  
Old 06-19-2008
Hi
Just want to know using perl.

This below one not working (printing blank lines only)

Code:
$file='ab.txt';
open(F,'ab.txt')|| die ("could not open file $file!");
while (<F>)
{
 chop;
@F=next if(m/(begin|end)/i);
print  "@F\n"
}

Thanks,
MR

Last edited by Yogesh Sawant; 06-23-2008 at 04:48 AM.. Reason: added code tags
# 6  
Old 06-19-2008
next skips to the following iteration. It does not return a useful value, and especially not a list containing a line you don't want to skip.

Code:
$file='ab.txt';
open(F,$file)|| die ("could not open file $file: $!");  # note minor changes in this line, too
while (<F>)
{
  # chop;  # Don't chop if you don't specifically need the line to be without a newline
  next if(m/(begin|end)/i);
  print;
}

In case you want the current line in a variable, the diamond operator <F> puts the current input line in $_, not in @F (in scalar context). chop, print, and the regex matching operators etc. all operate on $_ by default, i.e. unless you specify another variable to use.

In list context, <F> returns a list containing all the (remaining) input lines from the file handle. Here's an example of using list context:

Code:
print grep { ! /begin|end/ } <F>;

That is, grep the list of lines returned by <F>, picking out the ones which don't match the regex, and print them.

@F is only used if you have autosplit (-a option) in which case it contains the fields that the line was split into (on whitespace, unless you override it with the -F option).

Last edited by era; 06-19-2008 at 04:04 AM.. Reason: Explain when @F is useful, too
# 7  
Old 06-19-2008
Hi era,
I am really thankfull to you


Thanks,
MR
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

Using sed, awk or perl to remove substring of all lines except the first

Greetings All, I would like to find all occurences of a pattern and delete a substring from the all matching lines EXCEPT the first. For example: 1234::group:user1,user2,user3,blah1,blah2,blah3 2222::othergroup:user9,user8 4444::othergroup2:user3,blah,blah,user1 1234::group3:user5,user1 ... (11 Replies)
Discussion started by: jacksolm
11 Replies

3. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: siva kumar
3 Replies

4. Shell Programming and Scripting

remove blank lines and merge lines in shell

Hi, I'm not a expert in shell programming, so i've come here to take help from u gurus. I'm trying to tailor a csv file that i got to make it work for the LOAD FROM command. I've a datatable csv of the below format - --in file format xx,xx,xx ,xx , , , , ,,xx, xxxx,, ,, xxx,... (11 Replies)
Discussion started by: dvah
11 Replies

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

6. Shell Programming and Scripting

Command to remove duplicate lines with perl,sed,awk

Input: hello hello hello hello monkey donkey hello hello drink dance drink Output should be: hello hello monkey donkey drink dance (9 Replies)
Discussion started by: cola
9 Replies

7. Shell Programming and Scripting

perl or awk remove empty lines when condition

Hi Everyone, # cat 1 a b b cc 1 2 3 3 3 4 55 5 a b (2 Replies)
Discussion started by: jimmy_y
2 Replies

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

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

remove specific lines from flat file using perl

Hi, Here is wat im looking for.. i have a flat file which looks like this.. 00 * * * * .. .. * * text text text COL1 COL2 ----- ----- 1 a (12 Replies)
Discussion started by: meghana
12 Replies
Login or Register to Ask a Question