editing files inline, searching for \n\n\n

Hi,

I have got two versions of a script to eliminate single line-feeds from 
a file.  The first one does weird stuff - duplicating lines and messing 
the text file up.  The second one works (I copied it from a Perl guide), 
but I don't understand why.  I would much prefer the first one to work - 
Can you tell me how to change the first one to make it work?

Also, I understand that the <> operator reads in one line at a time.  If 
I wish to eliminate only triple line-feeds (\n\n\n) and leave double and 
single linefeeds, I presume <> won't work.  Without reading in the whole 
file at once, how can I achieve this?

I am using MacPerl.

Thanks for any help!

Tim

----------------------------------------------------

Version #1 (works dreadfully....stuffs up the file)

#! perl  -w -i
$filetobechanged = "iBook HD:Desktop Folder:tim.txt";
open(FILE, "+< $filetobechanged") ;
while (<FILE>) {
     s/\n//g;
     print FILE ;
         }
close(FILE);


Version #2 (works fine)
$filetobechanged = "iBook HD:Desktop Folder:tim.txt";
@ARGV = ($filetobechanged);
$^I = ".bak";
while (<>) {
     s/\n//g;;
     print;
}

0
tduke
5/10/2004 4:20:11 AM
perl.beginners 29388 articles. 4 followers. Follow

2 Replies
659 Views

Similar Articles

[PageSpeed] 10
Get it on Google Play
Get it on Apple App Store

Timothy Duke <tduke@itconnect.net.au> wrote:
: 
: Also, I understand that the <> operator reads in one
: line at a time.  If I wish to eliminate only triple
: line-feeds (\n\n\n) and leave double and single
: linefeeds, I presume <> won't work.  Without reading
: in the whole file at once, how can I achieve this?

    The diamond operator uses the record separator ($/)
to determine what a line ending looks like. By setting
this to "\n\n\n" we get "lines" that have "\n\n\n" at
the end. Conveniently, 'chomp' also uses $/ to figure
out what needs to be lopped off a line.

    It is best to limit a change in $/. The 'local'
function does this in a code block.

{
    local $/ = "\n\n\n";
    while ( <DATA> ) {
        chomp;
        print;
    }
}

# $/ back to its old value

__END__
 foo

 foo


 foo
 foo

HTH,

    Prints:
 foo

 foo foo
 foo

HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328

0
cclarkson
5/10/2004 11:12:20 AM
Timothy Duke wrote:
> 
> Hi,

Hello,

> I have got two versions of a script to eliminate single line-feeds from
> a file.

You should use chomp() to remove newlines.  Using the substitution
operator globally is forcing the whole line to be scanned.

> The first one does weird stuff - duplicating lines and messing
> the text file up.

That is because you are using '+<', the read/write mode.

> The second one works (I copied it from a Perl guide),
> but I don't understand why.

That is because Perl's <> operator and the $^I variable do all the work
behind the scenes of managing your file and the backup file.

> I would much prefer the first one to work -
> Can you tell me how to change the first one to make it work?

Yes.

> Also, I understand that the <> operator reads in one line at a time.

It opens one "line" at a time where a "line" is defined by the value of
the input record separator ($/) which is set to "\n" by default.

> If
> I wish to eliminate only triple line-feeds (\n\n\n) and leave double and
> single linefeeds, I presume <> won't work.  Without reading in the whole
> file at once, how can I achieve this?

Set the input record separator to paragraph mode.


> ----------------------------------------------------
> 
> Version #1 (works dreadfully....stuffs up the file)
> 
> #! perl  -w -i
> $filetobechanged = "iBook HD:Desktop Folder:tim.txt";
> open(FILE, "+< $filetobechanged") ;

You should *ALWAYS* verify that the file opened correctly.


> while (<FILE>) {
>      s/\n//g;
>      print FILE ;

In read/write mode you read line one and then print over line two and
then read line three and then print over line four, etc.


>          }
> close(FILE);
> 
> Version #2 (works fine)
> $filetobechanged = "iBook HD:Desktop Folder:tim.txt";
> @ARGV = ($filetobechanged);
> $^I = ".bak";
> while (<>) {
>      s/\n//g;;
>      print;
> }

If I understand correctly, you can achieve what you want like this:

#! perl -w
use strict;
( $/, $^I, @ARGV ) = ( '', '.bak', 'iBook HD:Desktop Folder:tim.txt' );
print while <>;


However, since you seem to want to open the files yourself, you could do
it like this:

#! perl -w
use strict;

my $filetobechanged = 'iBook HD:Desktop Folder:tim.txt';
my $backup_file = "$filetobechanged.bak";

rename $filetobechanged, $backup_file
    or die "Cannot rename $filetobechanged, $backup_file: $!";

open BACK, '<', $backup_file     or die "Cannot open $backup_file: $!";
open FILE, '>', $filetobechanged or die "Cannot open $filetobechanged:
$!";

$/ = '';  # Set paragraph mode

while ( <BACK> ) {
    print FILE;
    }

close BACK;
close FILE;

__END__



John
-- 
use Perl;
program
fulfillment
0
krahnj
5/10/2004 12:09:39 PM
Reply:

Similar Artilces:

inline editing of files, searching for \n\n\n
Hi, I have got two versions of a script to eliminate single line-feeds from a file. The first one does weird stuff - duplicating lines and messing the text file up. The second one works (I copied it from a Perl guide), but I don't understand why. I would much prefer the first one to work - Can you tell me how to change the first one to make it work? Also, I understand that the <> operator reads in one line at a time. If I wish to eliminate only triple line-feeds (\n\n\n) and leave double and single linefeeds, I presume <> won't work. Without reading i...

Display n to n of n records from a datagrid
Hello I have a datagrid with paging. This is working fine I would like to display something like 1 to 10 of 234 records I know how to get the 234 records but I don't know how to get the "1 to 10" or the "11 to 20", etc. Can anyone help me?  Hi,You need to get the PageIndex and the PageSize from your datagrid.  Then you can display the record numbers by multipying the PageSize by the PageIndex and adding one to get the start record number (i.e., 1 or 11), and then multiplying the PageSize by the PageIndex and adding the PageSize - 1 to get the end reco...

"\r\n\r\n" or "\n\n" or "\r\n" or "\n"... What is the term / name for this?
------=_NextPart_000_0011_01C353E5.0AE4D150 Content-Type: text/plain; charset="big5" Content-Transfer-Encoding: quoted-printable ------=_NextPart_000_0011_01C353E5.0AE4D150-- ...

RE: "\r\n\r\n" or "\n\n" or "\r\n" or "\n"... What is the term / name for this?
Escape characters that refer to: \r = carriage return \n = newline -----Original Message----- From: LI NGOK LAM [mailto:perl@reborn.org] Sent: Saturday, July 26, 2003 02:16 PM To: beginners@perl.org Subject: "\r\n\r\n" or "\n\n" or "\r\n" or "\n"... What is the term / name for this? Does anyone have a code snippet or a module for converting the IIS log = file format to NCSA Common? Cheers, Nigel ...

error opening file for writing:\r\n\r\nmozmapi32.dll\r\n\r\n
Has anyone found the answer to resolving this error msge on installing TB 2? tks andy amferrier@gmail.com wrote: > Has anyone found the answer to resolving this error msge on installing > TB 2? > > tks andy > Probably caused by your Logitech Webcam processes. Stop the Webcam process before installing. also to see more checkout http://kb.mozillazine.org/Thunderbird_2.0_installation_issues Brian ...

page n of n
Any news? I have a data window for a report and at the bottom of the report I have "page n of n". I get the correct number of pages but each page prints 1 of n where n may be 3 or 4; in the preview it is correct. The data window has a several report data windows embedded. Also, this worked fine under release 5. Any ideas? I think you might want to explain what type of datawindow you have, what the computed field is, etc... Mark -- Mark Pfeifer[Team PS] mpfeifer@sprynet.com I have this problem, too. The datawindow is a composit...

n sei n
Name: fabio Email: patoslocos157athotmaildotcom Product: Gran Paradiso Alpha 2 Summary: n sei n Comments: aaaaaaaaa Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a2) Gecko/20070206 GranParadiso/3.0a2 ...

Page n of N
I have been having a problem of the page numbers in a Report displaying correctly but not printing correctly. Instead of printing 1 of n, 2 of n, .... n of n, it has been printing x of n, x being the current page I am at in the datawindow. I did find this same problem reported earlier in this newsgroup and the datawindow newgroup. Does anyone have an answer to this problem? One of the suggestions was that we upgrade to 6.5.1, which can be done in my situation but a follow up said that this did not help. If anyone can help, I thank them in advance. ...

nôn
Name: thao Email: hothao87atyahoodotcomdotvn Product: Shiretoko Alpha 1 Summary: nôn Comments: very good Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1a1) Gecko/2008072310 Firefox/2.0.0.16;MEGAUPLOAD 1.0 From URL: http://www.mozilla.org/projects/firefox/3.1a1/firstrun/ ...

Ctrl+n or (File-New-Window ctrl+n)
Dear All,  I'm  having a problem with some users hitting CTRL-N to open a New Window. I'm not sure why they do this. Mostly, they say it is just habit or an accident. When they open the New Window, it takes on the duplicate of their current Session ID and HttpContext. When the Page does a PostBack, it causes almost unpredictable corruption of the Session data.Is there any way you can think to recognize this situation on the server side and therefore prevent it from happening? Thanks Jagan       G.JaganMohanRaoMumbaiIndia You should be ...

inplace editing problem: \n => \r\n
After inplace editing line ends may became different on dosish-like systems. Namely line ends for unix-like text files became dosish. I think it's unreasonable and unacceptable for binary files. ...

n-n table update
I have a n-n reference table [Pages_Linklists] that links a tables [Pages] to [Linklists]. This table only contains 2 fields: LinklistID and PageID. I my application, i want to display all Pages contained whithin a particular Linklist. So far no problem. The website users will have to be able to edit their Linklist, so I created a ListBox, in which i list all Pages whithin their Linklist. I also created the necessary buttons and dropdowns to add or remove pages. The problem is that i absolutely have to make batch updates, or when a new linklist is created, batch inserts. What ...

[Hendrix] m,n,n
Name: hhjh Product: eBay Companion Summary: m,n,n Comments: n,mn,m Browser Details: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14 From URL: http://www.mozilla.com/en-US/add-ons/ebay/feedback/ ...

Page n of n #3
This is a multi-part message in MIME format. --------------0983F82E8EA57F11733868C2 Content-Type: multipart/alternative; boundary="------------1DA6536D9D097C3764A38588" --------------1DA6536D9D097C3764A38588 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hi Gurus I am using PB6 with MS SQL 6.5 In my composite reports, page footer, (Page n of n) 'Page ' + page() + ' of ' + pagecount() is not properly printed. suppose the report has 10 pages, when It is previewed it shows 1 of 10, 2 of 10 and so on, means proper page no...

Web resources about - editing files inline, searching for \n\n\n - perl.beginners

String searching algorithm - Wikipedia, the free encyclopedia
In computer science , string searching algorithms , sometimes called string matching algorithms , are an important class of string algorithms ...

Facebook Becoming A Powerful Job Searching Tool
When most people think of a social media site that’s tailored to job searching and recruitment, the obvious answer seems to be LinkedIn . However, ...

Facebook Searching For Ways to Break Into Mobile Apps
Today’s mobile panel at the Facebook f8 developer conference was notable for the subject it didn’t cover: Facebook itself. With four panelists ...

Image Hunter - searching images on Google for iPad on the iTunes App Store
Read reviews, get customer ratings, see screenshots, and learn more about Image Hunter - searching images on Google on the App Store. Download ...

Searching - Flickr - Photo Sharing!
Advertising School: Miami Ad School, Miami Beach, USA Creative Director: Carlos Velasquez Art Director: Vanessa Castañeda Copywriter: Chris Baker ...

While searching for daughter, Va. girl's parents work to help others - YouTube
Washington Post While searching for daughter, Va. girl's parents work to help others Three months and three days. Thats how long it has been ...

Mixed martial arts searching for next generation of fighters
Chaqueal Waters is 167 centimetres tall, weighs 72 kilograms and, at just 16, he has made the decision to devote his energies to becoming the ...

Former renter searching for her mother's wedding dress from Mr Fluffy home
Robyn Elliott is moving&nbsp;on from&nbsp;the&nbsp;pain and&nbsp;dislocation caused by Canberra's Mr Fluffy asbestos crisis but more than a year ...

Family of Canadian killed while fighting ISIS in Syria still searching for answers
According to the Syrian Observatory for Human Rights, Canadian John Robert Gallagher was killed by an ISIS militant while fighting alongside ...

Family Still Searching For Answers After Christie Mullins' Cold Case Closed
The anguish is still fresh for Christie Mullins’ family, even 40 years later.

Resources last updated: 11/28/2015 10:58:40 AM