How to exclude lines from a txt file that start with a #

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jonathan184
    New Member
    • Nov 2006
    • 154

    How to exclude lines from a txt file that start with a #

    How to exclude lines from a txt file that start with a #

    Hi basically my script works fine.

    I wanted to add some comments in my config file but it is reading in the comment lines to.

    So basically i want to not read in the config lines in the script.

    This is the piece of the script that is reading in the lines in the array.
    Now it works fine without the if statement. So reason it is not working.

    Code:
    open(HAN, "sort_config.txt") || die " Unable to run date command $!";
        
      while (<HAN>) {
              $configline = <HAN>;
            # This if statement is to remove lines from the array that start with #
             chomp($configline);
             if ("$configline" =~ /^\#/) {
                print "not in array $configline \n";
                                         }
    else {
    push @array1, [ split ];  
    print "going in array $configline \n";
                      }
    close HAN;
                     }
    -------------------------------------------------------------
    Config files lines are:
    # CONFIG FILE ####### Delimiter is 1 space ***** DO NOT REMOVE LINE ****
    # SOURCE PATH DESTINATION PATH LOG PATH AND FILE ***** DO NOT REMOVE LINE ****
    # EXAMPLE: /home/source /home/archive /home/newlog.txt ***** DO NOT REMOVE LINE ****
    /home/source3 /home/archive /home/newlog3.txt
    /home/source4 /home/archive2 /home/newlog4.txt

    So basically i want the array to have only lines with no #
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Try replacing the if statement in the while loop to the following.

    [CODE=perl]#untested
    next if $configline =~ (/^#/);[/CODE]

    --Kevin

    Comment

    • pramodkh
      New Member
      • Nov 2007
      • 23

      #3
      Originally posted by eWish
      Try replacing the if statement in the while loop to the following.

      [CODE=perl]#untested
      next if $configline =~ (/^#/);[/CODE]

      --Kevin
      Yes...deffinate ly Kevin's reply should solve your problem. But i suggest you to use Config::IniFile s module to work with config files. You can get more details about this module in CPAN.

      Regards
      Pramod

      Comment

      Working...