Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9135433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:49:25+00:00 2026-06-17T08:49:25+00:00

I am trying to write a script which will count and erase empty lines

  • 0

I am trying to write a script which will count and erase empty lines from a file and save the changes in a new file:

if (@ARGV != 2) {
  print "Usage: $0 infile outfile\n";
  exit;
}
$infile = @ARGV[0];
$outfile = @ARGV[1];
open($old, "<$infile");
open($new, ">$outfile");
@mass = <$old>;
foreach $newc(@mass) {
    $counter++;
    if ($_ =~ /^$/) {
        print "blank line found in $old at line number $counter\n";
        print $new;
    }
}
close($new);
close($old);

But it’s not working. Where am I going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:49:26+00:00Added an answer on June 17, 2026 at 8:49 am

    Here’s another option:

    use strict;
    use warnings;
    
    @ARGV == 2 or die "Usage: $0 infile outfile\n";
    
    open my $fhIN,  '<', $ARGV[0] or die $!;
    open my $fhOUT, '>', $ARGV[1] or die $!;
    
    while (<$fhIN>) {
        if (/\S/) {
            print $fhOUT $_;
        }
        else {
            print "Blank at line $.\n";
        }
    }
    

    As amon showed, you can iterate over your file’s lines without first reading them into an array. This script also takes advantage of $., which contains the file’s current line number. The regex /\S/ checks for any non-whitespace characters in the line, as this indicates a non-blank line. If /\S/ is true, it writes the line to outfile, else it prints the blank-line notification.

    The file handles are lexically scoped in the three-argument form of open (the preferred method), so the files will automatically close at the script’s end.


    You can even go a step further and take advantage of STDIN, STDOUT and STDERR for maximum flexibility and usefulness.

    use strict;
    use warnings;
    
    while (<>) {
        if (/\S/) {
            print;
        }
        else {
            print STDERRR "Blank at line $.\n";
        }
    }
    

    Then just use

    script.pl file.in >file.out
    

    instead of

    script.pl file.in file.out
    

    but it also allows you to do stuff like

    prog1 | script.pl | prog2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a script which will modified 4 lines in InI
I'm trying to write a script which will read a file containing some urls
I'm trying to write a script which will convert all letters from lowercase to
I'm trying to write a very lightweight build script which will basically just get
I am trying to write a Java program or Hadoop Pig script which will
I am trying to write a script which uploads a file via a html
Hi all I am trying to write a script which recursively searches directories from
I am trying to write a script which will automate a copy/paste of employee
I am trying to write a ruby script which will look in a directory
I am trying to write an php twitter script which will be run by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.