LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to pull second line past keyword in a file into a shell variable. (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-pull-second-line-past-keyword-in-a-file-into-a-shell-variable-4175607475/)

usao 06-07-2017 11:45 AM

How to pull second line past keyword in a file into a shell variable.
 
I have a file which contains a keyword im looking for.
I need to pull the second line past the keyword into a shell variable.

Any suggestions on how to do this?

rtmistler 06-07-2017 12:22 PM

I usually do things the brute force way first and then figure out something more refined.

Or not. For something like this I may double grep, or use grep and cut.

For instance, I'd grep to find that line, providing it is a unique line in the file.

And then I'd use cut to remove all before or after. Another alternative is if there are delimiters, I'd use awk to treat it as column data and then select the correct column I needed.

Really what I'd do is go so far, and then web search for things like "grabbing the second field in a string using linux command line" and find examples and then choose the most comfortable one for me, that being the one I can best understand.

Habitual 06-07-2017 12:34 PM

Code:

Variable=$(grep -A2 <keyword> file | tail -1)
grep -A is the man page lookup for this exercise.

Have fun!

usao 06-07-2017 01:27 PM

Quote:

Originally Posted by Habitual (Post 5720196)
Code:

Variable=$(grep -A2 <keyword> file | tail -1)
grep -A is the man page lookup for this exercise.

Have fun!

Cool, didn't know grep could do that.

Habitual 06-07-2017 01:30 PM

Go TeamLQ!

Quote:

Originally Posted by usao (Post 5720207)
Cool

Cool is saying "Thank You".

So, we don't have to feel like "support"?

You're Welcome.


All times are GMT -5. The time now is 05:20 AM.