Removing new line character from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chittaranjan
    New Member
    • Sep 2006
    • 51

    Removing new line character from a string

    Hi All,

    I am using perl to get the data from the form fields and I have a text area from where if some one enters data like below:

    "This is test
    And this is also test
    This is also test."

    So here you can notify the text is entered with hitting of enter key to have the line break. But I am having the problem while saving the data entered in this field in a text file then there also it is saving with these line breaks(new lines) so if am trying to upload the file to the fields then it is saying that invalid file.

    So I was thinking is there a way in perl to remove the new line character from the string while saving the form data. So that I will not have the problem while uploading.

    Please let me know with some sample examples to solve this I will be waiting for all your valuable responses.

    Thanks,
    Chittaranjan :)
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by Chittaranjan
    Hi All,

    I am using perl to get the data from the form fields and I have a text area from where if some one enters data like below:

    "This is test
    And this is also test
    This is also test."

    So here you can notify the text is entered with hitting of enter key to have the line break. But I am having the problem while saving the data entered in this field in a text file then there also it is saving with these line breaks(new lines) so if am trying to upload the file to the fields then it is saying that invalid file.

    So I was thinking is there a way in perl to remove the new line character from the string while saving the form data. So that I will not have the problem while uploading.

    Please let me know with some sample examples to solve this I will be waiting for all your valuable responses.

    Thanks,
    Chittaranjan :)
    You will want to read up on the chomp() function. That does what you want.

    Regards,

    Jeff

    Comment

    • micwood
      New Member
      • Nov 2007
      • 4

      #3
      I have a simpler Perl question, but chomp doesn't seem to be doing the trick.

      I've input a file in paragraph mode (ie set $/="") as much of the data i need to isolate is multi-line. I have set up a while (<>) loop, in which I have several if statements to pattern match. I am saving long text strings into a hash but I need the values not to have any embedded "/n", such as:

      Justice Hudson did not participate in the consideration or decision of\n
      this case.

      I need the entire sentence (ie the entire string) as a value in a hash with no embedded "\n". I can isolate the pattern fine, and I can assign this string to a hash value, but no matter what i do I can't remove that "\n" since it is embedded in a string (opposed to being at the end, which would be no problem to remove).

      Any suggestions?

      Thanks a bunch!

      Michael

      Comment

      • micwood
        New Member
        • Nov 2007
        • 4

        #4
        I should also note that once i have isolated the entire string (with embedded "\n") to a hash value, say $case{'judges'} for example, and i try the following after the while (<>) loop:

        if (exists $case{'judges'} ){
        $case{'judges'} =~s/\n//g;}

        i get the following when i try to print $case{'judges'} :

        Justice Hudthis case.t participate in the consideration or decision of


        which just makes me sad. :-(
        Any idea what i am doing wrong?
        Thanks, again.
        Michael

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          There is something else going on. As you can see by this example, removing newlines is no problem:

          Code:
          my $test = qq{this
          is a
          test
          };
          print "before:\n";
          print $test;
          $test =~ s/\n/ /g;
          print "\nafter:\n";
          print $test;

          Comment

          • micwood
            New Member
            • Nov 2007
            • 4

            #6
            Thanks for your help but I still am apparently breaking Perl. I tried your test and yes, it works. But when I try to apply it to my script, all hell breaks lose. At its most basic, this is my script:

            [code=perl]
            #!/usr/bin/perl -w
            my %case = (); # case hash
            my $judges = ''; # temp key
            open (IN, "/Users/MicWood/Documents/Perl/Opinion\ Scripts/output.txt");
            $/=""; # paragraph input mode
            while (<IN>) {
            if (/^JUDGES:\s(.+)\ n\n/s){
            $case{'judges'} =$1};
            next;}
            print "before:\n" ;
            print $case{'judges'} ;
            $case{'judges'} =~ s/\n/ /;
            print "\nafter:\n ";
            print $case{'judges'} ;
            print "\n";
            [/code]

            The result between the two prompts:
            before:
            Justice HUDSON did not participate in the consideration or decision of
            this case.
            after:
            this case.SON did not participate in the consideration or decision of



            Why is it transposing the two parts of the string?

            Thanks again!
            Michael

            Comment

            • micwood
              New Member
              • Nov 2007
              • 4

              #7
              Whoops:
              that should be:
              $case{'judges'} =~ s/\n/ /g;

              not
              $case{'judges'} =~ s/\n/ /;


              But same transposed result as before.

              Thanks
              Michael

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Maybe something do with the way you are printing the output while still in the loop, try like this:


                [code=perl]
                #!/usr/bin/perl -w
                my %case = (); # case hash
                my $judges = ''; # temp key
                open (IN, "/Users/MicWood/Documents/Perl/Opinion\ Scripts/output.txt");
                $/=""; # paragraph input mode
                while (<IN>) {
                if (/^JUDGES:\s(.+)\ n\n/s) {
                $case{'judges'} =$1;
                last;
                }
                close (IN);
                print "before:\n" ;
                print $case{'judges'} ;
                $case{'judges'} =~ s/\n/ /;
                print "\nafter:\n ";
                print $case{'judges'} ;
                print "\n";
                [/code]

                If that does not help, post the input file or attach it to a post.

                Comment

                • hulk789
                  New Member
                  • Jul 2015
                  • 1

                  #9
                  $test =~ s/\n/ /g;

                  can you explain this is this find and replace kind of thing

                  Comment

                  Working...