ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Solved Searching for text in file

    IT Discussion
    linux grep bash
    4
    7
    844
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 1
      1337
      last edited by

      If you have a text file that looks like this:

      start_folder='/folder1/abc.txt'
      iterations='123'
      passphrase='xyz'
      last_command='invoke'
      return_value='0'
      

      How can you pick out just xyz when looking for "passphrase"?

      I know grep will get me the line but what should I use if I want just a part of the line?
      Can it be done in one command or do I have to pipe several together?

      dafyreD 1 Reply Last reply Reply Quote 0
      • dafyreD
        dafyre @1337
        last edited by

        @Pete-S said in Searching for text in file:

        If you have a text file that looks like this:

        start_folder='/folder1/abc.txt'
        iterations='123'
        passphrase='xyz'
        last_command='invoke'
        return_value='0'
        

        How can you pick out just xyz when looking for "passphrase"?

        I know grep will get me the line but what should I use if I want just a part of the line?
        Can it be done in one command or do I have to pipe several together?

        If you the text has a character that would be a good delimiter, you can pipe grep to cut... ie:

        cat myfile.txt|grep "iterations"|cut -d '=' -f 2
        Output:
        '123'
        

        the -f # is which column you want.

        There may be other ways to do it, but that's the first way I can think of.

        1 ObsolesceO 2 Replies Last reply Reply Quote 1
        • 1
          1337 @dafyre
          last edited by 1337

          @dafyre said in Searching for text in file:

          @Pete-S said in Searching for text in file:

          If you have a text file that looks like this:

          start_folder='/folder1/abc.txt'
          iterations='123'
          passphrase='xyz'
          last_command='invoke'
          return_value='0'
          

          How can you pick out just xyz when looking for "passphrase"?

          I know grep will get me the line but what should I use if I want just a part of the line?
          Can it be done in one command or do I have to pipe several together?

          If you the text has a character that would be a good delimiter, you can pipe grep to cut... ie:

          cat myfile.txt|grep "iterations"|cut -d '=' -f 2
          Output:
          '123'
          

          the -f # is which column you want.

          There may be other ways to do it, but that's the first way I can think of.

          I also need to exclude the ' from the end result. So 123 and not '123'.
          So the first and the last character has to be removed as well.

          1 Reply Last reply Reply Quote 0
          • stacksofplatesS
            stacksofplates
            last edited by

            @Pete-S said in Searching for text in file:

            grep "iterations"|cut -d '=' -f 2

            Just use trim.

            grep "passphrase" file.txt |cut -d '=' -f 2 | tr -d \'
            
            1 Reply Last reply Reply Quote 4
            • 1
              1337
              last edited by

              Thanks @dafyre @stacksofplates !

              1 Reply Last reply Reply Quote 0
              • ObsolesceO
                Obsolesce @dafyre
                last edited by Obsolesce

                @dafyre said in Searching for text in file:

                @Pete-S said in Searching for text in file:

                If you have a text file that looks like this:

                start_folder='/folder1/abc.txt'
                iterations='123'
                passphrase='xyz'
                last_command='invoke'
                return_value='0'
                

                How can you pick out just xyz when looking for "passphrase"?

                I know grep will get me the line but what should I use if I want just a part of the line?
                Can it be done in one command or do I have to pipe several together?

                If you the text has a character that would be a good delimiter, you can pipe grep to cut... ie:

                cat myfile.txt|grep "iterations"|cut -d '=' -f 2
                Output:
                '123'
                

                the -f # is which column you want.

                There may be other ways to do it, but that's the first way I can think of.

                You can specify a file with grep, no need to pipe in from cat.

                dafyreD 1 Reply Last reply Reply Quote 1
                • dafyreD
                  dafyre @Obsolesce
                  last edited by

                  @Obsolesce said in Searching for text in file:

                  @dafyre said in Searching for text in file:

                  @Pete-S said in Searching for text in file:

                  If you have a text file that looks like this:

                  start_folder='/folder1/abc.txt'
                  iterations='123'
                  passphrase='xyz'
                  last_command='invoke'
                  return_value='0'
                  

                  How can you pick out just xyz when looking for "passphrase"?

                  I know grep will get me the line but what should I use if I want just a part of the line?
                  Can it be done in one command or do I have to pipe several together?

                  If you the text has a character that would be a good delimiter, you can pipe grep to cut... ie:

                  cat myfile.txt|grep "iterations"|cut -d '=' -f 2
                  Output:
                  '123'
                  

                  the -f # is which column you want.

                  There may be other ways to do it, but that's the first way I can think of.

                  You can specify a file with grep, no need to pipe in from cat.

                  This is true! I always seem to get it backwards when I do that, so i just cat $thefile | grep | blah ... Cuts down on frustration, ha ha.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post