473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Remove last new line

11
Hi,
I have a slight problem which is probably easy to fix, but I am still fairly new to this language. Firstly, I shall show you the code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. opendir(DIR, "directory") || die "Cannot open directory";
  4. my @file = readdir(DIR);
  5. closedir(DIR);
  6. open(NF, ">file" || die "Cannot open file");
  7.  
  8. foreach my $file (@file)
  9. {
  10.     @file = sort(@file);
  11.     open(FH, $file) or die "$!";
  12.  
  13.     while (<FH>)
  14.     {
  15.         if ((/Jan-\d\d/) || (/Feb-\d\d/) || (/Mar-\d\d/) || (/Apr-\d\d/) || (/May-\d\d/) || (/Jun-\d\d/) || (/Jul-\d\d/) || (/Aug-\d\d/) || (/Sep-\d\d/) || (/Oct-\d\d/) || (/Nov-\d\d/) || (/Dec-\d\d/))
  16.         {
  17.         chomp;
  18.         print NF "$_\n";
  19.         }
  20.     }
  21. }
  22. close(NF);
  23. close(FH);
  24.  
As you can see, I am printing the lines of the file with each line on a new line. However, I would like to somehow tell it not to make a new line if it is the last line in the file. I have tried removing it through the command prompt, and even tried manually removing it using a text editor, but it is always there. I am using this file to plot data, and as such there must not be a new line at the end because the values for that line will be 0. Any help would be appreciated, thanks in advance.
Oct 11 '07 #1
3 2968
Maybe

Expand|Select|Wrap|Line Numbers
  1. print NF "$_\n" if length $_ > 0;
?
Oct 11 '07 #2
KevinADC
4,059 Expert 2GB
untested but should work:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. opendir(DIR, "directory") || die "Cannot open directory";
  7. my @file = readdir(DIR);
  8. closedir(DIR);
  9. @file = sort(@file);
  10. open(NF, ">file" || die "Cannot open file");
  11.  
  12. foreach my $file (@file) {
  13.    my $first_line = 1;
  14.    open(FH, $file) or die "$!";
  15.    while (<FH>) {
  16.       chomp;
  17.       if ((/Jan-\d\d/) || (/Feb-\d\d/) || (/Mar-\d\d/) || (/Apr-\d\d/) || (/May-\d\d/) || (/Jun-\d\d/) || (/Jul-\d\d/) || (/Aug-\d\d/) || (/Sep-\d\d/) || (/Oct-\d\d/) || (/Nov-\d\d/) || (/Dec-\d\d/)) {
  18.          if ( $first_line ) {
  19.             print NF $_;
  20.             $first_line = 0;
  21.          }
  22.          else {
  23.             print NF "\n$_";
  24.          }
  25.       }
  26.    }
  27. }
  28. close(NF);
  29. close(FH);
Oct 11 '07 #3
dh87lfc
11
I shall try that tomorrow. Thanks.
Oct 11 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Joram Agten | last post by:
Please put me in CC When running the following program I get frequent errors like this one Exception in thread Thread-4: Traceback (most recent call last): File...
3
by: Li Pang | last post by:
Hi, I want to know the easiest way to remove the last line in a text file, or how to catch the last line of a text file. Thanks in advance
6
by: Daniel Mark | last post by:
Hello all: I have the following snippet: In : fileName = 'Perfect Setup.txt\n' In : fileName = fileName # remove the '\n' character In : fileName Out: 'Perfect Setup.txt'
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
4
by: Franky | last post by:
What I want to do is delete the last line in a RichTextBox. The RichTextBox has a ReadOnly property called lines that seems like it might help but I cant figure out how to use it. Well, the...
9
by: Frank Potter | last post by:
I only want to remove the comments which begin with "//". I did like this, but it doesn't work. r=re.compile(ur"//+$", re.UNICODE|re.VERBOSE) f=file.open("mycpp.cpp","r") f=unicode(f,"utf8")...
4
by: Yansky | last post by:
Got a quick n00b question. What's the difference between del and remove?
10
by: Mike Copeland | last post by:
I have data I need to normalize - it's "name" data. For example, I have the following: "Watts, J.C." I wish to (1) parse the "first name" ("J.C.") and adjust it to "JC". Essentially, I want to...
8
by: moondaddy | last post by:
I have a .net 3.0 wpf app and using c# I need to do 2 things: 1) Get the last point in the path, or get the last linesegment in the path where I could then get the last point. 2) Remove the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.