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?



  • @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.



  • @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.



  • @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 \'




  • @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.



  • @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.


Log in to reply