LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [Shell script] How to read, filter and split from text file (https://www.linuxquestions.org/questions/linux-newbie-8/%5Bshell-script%5D-how-to-read-filter-and-split-from-text-file-4175562849/)

Mr.Tee 12-31-2015 09:32 PM

[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.

Mr.Tee 12-31-2015 10:38 PM

I solved this problem.
Use: grep "kernel_1" abc.txt | awk -F "=" '{print $2}'

syg00 12-31-2015 10:48 PM

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.

grail 01-01-2016 01:33 AM

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"


Mr.Tee 01-01-2016 03:00 AM

Thank all you very much for your help.
I will try with your recommendation.
Have a nice day.
Tee.


All times are GMT -5. The time now is 08:14 PM.