LinuxQuestions.org
Help answer threads with 0 replies.
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 12-31-2015, 10:32 PM   #1
Mr.Tee
LQ Newbie
 
Registered: Dec 2015
Posts: 3

Rep: Reputation: Disabled
[Shell script] How to read, filter and split from text file


Hi all,
First, I am sorry if my question duplicate with someone before.
Second, I am trying to create a shell script which will read a text file and give me information from text file.
Example:
$cat abc.txt
android_1=/home/abc/xyz/source/android
kernel_1=/home/abc/xyz/source/android/kernel/board1
...
... and so on

I want to create shell script which get directory information from abc.txt such as return "/home/abc/xyz/source/android"

How can I do that, any one help me please....

Too many thanks.
 
Old 12-31-2015, 11:38 PM   #2
Mr.Tee
LQ Newbie
 
Registered: Dec 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
I solved this problem.
Use: grep "kernel_1" abc.txt | awk -F "=" '{print $2}'
 
Old 12-31-2015, 11:48 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,834

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
There are lots of ways of doing this. For your solution, there is no need for grep - awk can do the match and print when found. Saves shelling out to grep to read the whole file then read the grep output with awk.

Could also be done entirely in bash with parameter substitution.
 
Old 01-01-2016, 02:33 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
If your whole file looks like the example, you could also simply source the file and use the variables from the file:
Code:
source abc.txt

echo "$android_1"
 
Old 01-01-2016, 04:00 AM   #5
Mr.Tee
LQ Newbie
 
Registered: Dec 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank all you very much for your help.
I will try with your recommendation.
Have a nice day.
Tee.
 
  


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
shell script to read a certain segment of a text file? recomboDNA Programming 3 06-16-2010 02:57 AM
Cannot read text from text file and store it in a variable using shell script anurupr Linux - Newbie 2 03-03-2010 02:38 PM
Shell script for read user data with emptyLines in a text file and filter them srimal Linux - Newbie 7 11-01-2009 05:37 AM
Shell script to read lines in a text file and filter user data srimal Linux - Newbie 5 10-21-2009 08:41 AM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 03:10 PM


All times are GMT -5. The time now is 06:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration