LinuxQuestions.org
Review your favorite Linux distribution.
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, 09: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, 10: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, 10:48 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,249

Rep: Reputation: 4156Reputation: 4156Reputation: 4156Reputation: 4156Reputation: 4156Reputation: 4156Reputation: 4156Reputation: 4156Reputation: 4156Reputation: 4156Reputation: 4156
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, 01:33 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018

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

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

All times are GMT -5. The time now is 05:49 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