Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
cancel
Showing results for 
Search instead for 
Did you mean: 

perl delete help

SOLVED
Go to solution
Shaf_1
Advisor

perl delete help

I have some files that I need to delete the header information. I am new to Perl and have no idea on how to approach this. I have attached an example of the files I am working with. I need to be able to delete the header information starting from 'Received:' all the way up to 'DeliveredDate:'. Can anyone please help? Thanks
7 REPLIES
John Poff
Honored Contributor
Solution

Re: perl delete help

Hi,

Here is one way to do it. Create a Perl script that will match a range of lines and skip them, otherwise printing the lines. Something like this:

#!/usr/bin/perl

# noheader.pl

while (<>) {
if (/Received:/ .. /DeliveredDate:/) {
next;
} else {
print;
}
}

Now, just run it like this:

./noheader.pl <159191.txt

and it should just print the text you need.

Of course, there are even shorter ways to do it. After I played with this for a bit I figured out that this would also work:

while (<>) {
print unless (/Received:/ .. /DeliveredDate:/);
}



JP

Shaf_1
Advisor

Re: perl delete help

Thank you so much for the help. However, I have a couple of more questions now. When the data is printed how can I save the new output in the same file?

In perl can I have the script to run for over 500 different files at once? Is there a way to avoid entering the file name for every file?
John Poff
Honored Contributor

Re: perl delete help

Hi,

You can use the '-i' option from the Perl command line to edit the file in place. Something like this should work:

perl -i -n -e 'print unless (/Received:/ .. /DeliveredDate:/);' 159191.txt


There are a couple of ways to handle it with a bunch of files. One way would be to put the line above into a small shell script:

for f in *.txt
do
perl -i -n -e 'print unless (/Received:/ .. /DeliveredDate:/);' $f
done


You could also use the file handling machinery inside of Perl and do it in a single script.

JP
Shaf_1
Advisor

Re: perl delete help

Hello,

I put the following in a script as you suggested:

for $f in *.txt
do
perl -i -n -e 'print unless (/Received:/ .. /DeliveredDate:/);'$f
done

I get this error message:

'Missing $ on loop variable at test.pl line 1.'

Am I doing something wrong? Do I need to assign a directory where the files are located?

Thanks
John Poff
Honored Contributor

Re: perl delete help

Hi,

It looks like you left out the space between the last single quote and the dollar f variable.

You should be able to run this from the directory where your files are located, or else you can specify the path in the 'for' statement, like this:

for f in /some/directory/*.txt

JP
Shaf_1
Advisor

Re: perl delete help

Hello JP,

Sorry about this I have another question. Can I put this code in a .pl file:

for f in *.txt
do
'print unless(/Received:/ .. /DeliveredDate:/);' $f
done

and then run it this way:

perl -i -n -e filename.pl

I am not sure if this is legal or not.

Thanks



Shaf_1
Advisor

Re: perl delete help

Hello JP,

Please disregard my previous message. I got it to work. Thank you for all your help. You saved me!