LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-07-2017, 12:45 PM   #1
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 286

Rep: Reputation: Disabled
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?
 
Old 06-07-2017, 01:22 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,900
Blog Entries: 13

Rep: Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947
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.
 
Old 06-07-2017, 01:34 PM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

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

Have fun!
 
1 members found this post helpful.
Old 06-07-2017, 02:27 PM   #4
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 286

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
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.
 
Old 06-07-2017, 02:30 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Go TeamLQ!

Quote:
Originally Posted by usao View Post
Cool
Cool is saying "Thank You".

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

You're Welcome.

Last edited by Habitual; 06-07-2017 at 04:06 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ksh read variable from text file and populate with content of shell variable WindozBytes Programming 4 09-17-2012 02:48 PM
[SOLVED] Pull string from a file into a variable using bash DuskFall Linux - Newbie 13 08-06-2011 04:06 PM
Shell to remove line matched with variable myosopia Programming 4 07-25-2011 12:16 PM
BASH shell question, variable sustitution $1... command line args mnemry Linux - Newbie 8 04-19-2011 05:40 PM
Need assistance with shell script to pull MP3 file Cpare Linux - Newbie 5 06-28-2008 09:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration