How to get the inbetween lines in a file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dheenaalex
    New Member
    • Oct 2008
    • 13

    How to get the inbetween lines in a file?

    Hi Every One,

    I am having a file like this..

    start
    bla bla bla1
    start1
    bla bla bla2
    bla bla bla3
    end1
    bla bla bla4
    bla bla bla5
    start2
    bla bla bla6
    bla bla bla7
    end2
    bla bla bla8
    end
    from this i want to get the lines inbetween start1 and end1 i.e what i want is as follows

    bla bla bla2
    bla bla bla3

    like this the file has n number of sets..

    can any guide me regarding this?

    Thanks and Regards,
    Dheena.
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    Originally posted by dheenaalex
    Hi Every One,

    I am having a file like this..

    start
    bla bla bla1
    start1
    bla bla bla2
    bla bla bla3
    end1
    bla bla bla4
    bla bla bla5
    start2
    bla bla bla6
    bla bla bla7
    end2
    bla bla bla8
    end
    from this i want to get the lines inbetween start1 and end1 i.e what i want is as follows

    bla bla bla2
    bla bla bla3

    like this the file has n number of sets..

    can any guide me regarding this?

    Thanks and Regards,
    Dheena.
    What have you tried so far? You can make use of regex to skip the lines until a criteria is met and to print lines until other criteria is met.

    Comment

    • dheenaalex
      New Member
      • Oct 2008
      • 13

      #3
      Hi Nithinpes,

      yes i know the regular expressions..

      But the regular expressions will work with a single string..

      what i need is, i want to get the lines in between those lines(start1 and end1), like that its having n number..

      I want to get those strings in a array of string (i.e) inbetween those two lines..

      Thanks and Regards,
      Dheenaalex

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by dheenaalex
        Hi Nithinpes,

        yes i know the regular expressions..

        But the regular expressions will work with a single string..

        what i need is, i want to get the lines in between those lines(start1 and end1), like that its having n number..

        I want to get those strings in a array of string (i.e) inbetween those two lines..

        Thanks and Regards,
        Dheenaalex
        To get lines between start1 and end1(e.g), try the following:
        Code:
        open(F,"z.txt") or die "error:$!";
        while(<F>) {
        next until /start1/;
        @rem=<F>;
        }
        foreach(@rem){
        last if(/end1/);
        print $_;
        }
        - Nithin

        Comment

        • dheenaalex
          New Member
          • Oct 2008
          • 13

          #5
          Hi Nithin,

          This will work only for the start 1 and end1... actually i want between start2 and end2 like this n number of blocks..

          Let me give a detailed explation, abt what i need..

          My file is as follows

          begin
          bla bla bla1
          bla bla bla2
          start
          bla bla bla3
          bla bla bla4
          end
          bla bla bla5
          start
          bla bla bla6
          bla bla bla7
          bla bla bla8
          end
          bla bla bla9
          end

          I want the output as follows..

          In the first string array i want bla bla bla3, bla bla bla4
          In the second string array i want bla bla bla6, bla bla bla7 and bla bla bla8.

          Like this it may have n number of start and end block.

          Thanks and Regards,
          Dheena.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Why is this person allowed to continue posting?

            He shows no effort.

            He has never answered if this is school work.

            His question is answered in the perl FAQs that come with perl but can't even be bothered to read them? Probably doesn't even know they exist.

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              dheenaalex,

              To get help, you are going to show us that you are actually trying to write code for your issue. Granted, Nithinpes did provide you some code to give you a boost, but you need to please post your code here (above and beyond what you were given by Nithinpes) so that we can help you.

              Unless we know what you are trying and that you are actually coding and not expecting the answer, then I don't know what we can do much more for you.

              Regards,

              Jeff

              Comment

              • dheenaalex
                New Member
                • Oct 2008
                • 13

                #8
                Hi Nithin,

                Now I got that how to get the lines inbetween two lines.. now i want other than that lines..
                .

                The code for inbetween lines are as follows..

                Code:
                 open(TEXT, "test.txt");
                	@contents = <TEXT>;
                						
                		foreach (@contents){
                						
                			if(/$start/ .. /end/) {
                						
                				push @bet,$_;
                						
                			}	 					
                		}

                So i am trying to get the lines other than that inbetween lines, for that i am using the following code, but its not working.. can you guide me regarding this.

                Code:
                 open(TEXT, "test.txt");
                	@contents = <TEXT>;
                						
                		foreach (@contents){
                						
                			if(/$start/ .. /end/) {
                						
                				push @bet,$_;
                						
                			}	else {
                			
                					push @other,$_;
                				
                			} 					
                		}
                This was the file what i am having...

                begin
                bla bla bla1
                bla bla bla2
                start
                bla bla bla3
                bla bla bla4
                end
                bla bla bla5
                start
                bla bla bla6
                bla bla bla7
                bla bla bla8
                end
                bla bla bla9
                end

                Now what i want is other than inbetween lines, is as follows..

                bla bla bla1
                bla bla bla2

                bla bla bla5

                bla bla bla9


                Then one thing Nithin, one guy was always scolding me.. Posting points against me..Pls tel him to stop posting like this..., if i am telling means he wont..

                Regards,
                Dheena

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  You are a bad boy, a very very bad boy. You have to go to the principles office you bad boy.

                  Comment

                  • nithinpes
                    Recognized Expert Contributor
                    • Dec 2007
                    • 410

                    #10
                    Originally posted by dheenaalex
                    Hi Nithin,

                    Now I got that how to get the lines inbetween two lines.. now i want other than that lines..

                    Regards,
                    Dheena
                    The code you posted works fine for me, expect for the fact that includes 'begin' and 'end' lines. If you want to skip those, use a next statement:

                    Code:
                    open(TEXT, "output.txt"); 
                        @contents = <TEXT>; 
                      
                            foreach (@contents){ 
                                
                                if(/start/ .. /end/) { 
                                    next if((/start/ )|| (/end/) ||(/begin/));
                                    push @bet,$_; 
                      
                                }    else { 
                                        next if((/start/ )|| (/end/) ||(/begin/));
                                        push @other,$_; 
                      
                                }                      
                            }    
                    print "@other";

                    Comment

                    • nithinpes
                      Recognized Expert Contributor
                      • Dec 2007
                      • 410

                      #11
                      Originally posted by dheenaalex
                      Hi Nithin,

                      Then one thing Nithin, one guy was always scolding me.. Posting points against me..Pls tel him to stop posting like this..., if i am telling means he wont..

                      Regards,
                      Dheena
                      That is so childish....
                      You never replied to the question in your previous thread on whether it was your homework. Also, in this thread, you were reluctant to post your code.
                      Please remember in future,that you need to post your code to get replies/ suggestions.

                      Regards,
                      Nithin

                      Comment

                      • dheenaalex
                        New Member
                        • Oct 2008
                        • 13

                        #12
                        Hi Kevin,

                        As your wish..

                        Regards,
                        Dheena.

                        Comment

                        • dheenaalex
                          New Member
                          • Oct 2008
                          • 13

                          #13
                          HI Nithin,

                          ok.. Just for fun i posted like that.

                          Sorry for that..

                          Ok now what i want is i am appending a file ..

                          In the run time i want to check for the data..

                          Actually what i need is while appending data in to a file, i want to check weather the data is already or not..

                          Is it possible in perl?



                          Regards,
                          Dheena

                          Comment

                          • nithinpes
                            Recognized Expert Contributor
                            • Dec 2007
                            • 410

                            #14
                            Originally posted by dheenaalex
                            HI Nithin,

                            ok.. Just for fun i posted like that.

                            Sorry for that..

                            Ok now what i want is i am appending a file ..

                            In the run time i want to check for the data..

                            Actually what i need is while appending data in to a file, i want to check weather the data is already or not..

                            Is it possible in perl?



                            Regards,
                            Dheena
                            You can avoid duplicate lines while appending/writing to a file by making use of a hash. For example,
                            Code:
                            open(TEXT, "in.txt"); 
                            open(NEW, ">>out.txt");
                            my @data=<TEXT>;
                            
                            foreach(@data) {
                               print NEW unless($seen{$_}++); ## append unless the data exists already
                            }
                            - Nithin

                            Comment

                            • KevinADC
                              Recognized Expert Specialist
                              • Jan 2007
                              • 4092

                              #15
                              Originally posted by nithinpes
                              You can avoid duplicate lines while appending/writing to a file by making use of a hash. For example,
                              Code:
                              open(TEXT, "in.txt"); 
                              open(NEW, ">>out.txt");
                              my @data=<TEXT>;
                              
                              foreach(@data) {
                                 print NEW unless($seen{$_}++); ## append unless the data exists already
                              }
                              - Nithin
                              And that is also covered in the perl FAQs. Its obvious this guy is way lazy, but its not really my business if you wish to continue helping him.

                              Kevin

                              Comment

                              Working...