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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
08-17-2016, 06:59 AM
|
#1
|
LQ Newbie
Registered: May 2016
Posts: 10
Rep: 
|
Extracting lines from a file using shell scripting
I am trying to read a text file using bash shell scripting based on certain conditions.
Sample File Content -
Application Name: xyz
Service Name: Process Archive.par
Deployment Status: Success
Service Instance Name: Process Archive
Machine Name: abc
Status: Running
Condition - Print Application Name having Status as Running
I am able to read file using while IFS and tried different awk combinations also. But nothing worked. The sample file content contains spaces and tab as this is the result of another script I made.
Please help. Thanks in advance.
|
|
|
08-17-2016, 07:46 AM
|
#2
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,042
|
Please show us your attempt? I would add that you need nothing more than bash, ie. you do not need awk, sed or any other helper.
|
|
2 members found this post helpful.
|
08-17-2016, 01:22 PM
|
#3
|
Member
Registered: Nov 2003
Distribution: Fedora, Debian, Ubuntu, FreeBSD
Posts: 129
Rep:
|
Are you interested in something like this:
Code:
$ grep 'Application Name\|^Status' sample.txt
(Since the string 'Status' is repeated twice, I use the "^" to grep where the string matches the starting position.)
|
|
|
08-17-2016, 01:29 PM
|
#4
|
Senior Member
Registered: Sep 2010
Location: Lawrence, New Zealand
Distribution: Slackware
Posts: 1,077
|
You may also be interested in `pgrep`. Not a direct answer to your question, but related to what you appear to be trying to do.
|
|
|
08-17-2016, 04:45 PM
|
#5
|
LQ Guru
Registered: Oct 2004
Distribution: Arch
Posts: 5,523
|
Code:
#For list of files
files="
file1.txt
file2.txt
file3.txt
"
for i in $files; do
#For whole directory
for i in *.txt; do
if grep -q "Status: Running" $i; then
echo $i
fi
done
|
|
|
08-18-2016, 03:37 AM
|
#6
|
LQ Newbie
Registered: May 2016
Posts: 10
Original Poster
Rep: 
|
I tried with the following
for i in `cat $1`; do
if grep -q " Status: Running" $i; then
echo $($i-5)
fi
done
As I wanted to display the application name when the status is running, I echo ($i-5).
But this is not displaying the expected output.
Below is the output of the above code snippet.
======
grep: Deployment: No such file or directory
grep: Status:: No such file or directory
grep: Success: No such file or directory
grep: Service: No such file or directory
grep: Instance: No such file or directory
=======
|
|
|
08-18-2016, 03:49 AM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,676
|
what is $1 in your last post?
Please use [code]here comes your code[/code] to keep formatting
|
|
|
08-18-2016, 03:54 AM
|
#8
|
LQ Newbie
Registered: May 2016
Posts: 10
Original Poster
Rep: 
|
Below command is producing Application Name as output. But, it is not satisfying the condition.
Command - grep 'Application Name\|^Status' sample.txt
Output -
Application Name: Vabc/xyz
Application Name: Atest/test
The status of Application Atest is Stopped. This should not be in the output.
I tried few combinations.
Command - grep 'Application Name\| Status: Running' sample.txt
Output -
Application Name: Vadiraj/CoreToDTS
Status: Running
Application Name: Atest/testingzipping
I specifically need only Vabc/xyz as output as this application satisfies the condition.I may later try trimming it. But the above command is listing all application names irrespective of the condition.
|
|
|
08-18-2016, 03:57 AM
|
#9
|
LQ Newbie
Registered: May 2016
Posts: 10
Original Poster
Rep: 
|
$1 is the argument that I am passing.
./ListApp.sh sample.txt
Content of sample.txt
Initializing ...
Finished initialization
Application Name: Vabc/xyz
Service Name: Process Archive.par
Deployment Status: Success
Service Instance Name: Process Archive
Machine Name: abc
Status: Running
Application Name: Atest/test
Service Name: Process Archive.par
Deployment Status: Success
Service Instance Name: Process Archive
Machine Name: xyz
Status: Stopped
|
|
|
08-18-2016, 04:10 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,676
|
I would use awk instead of grep:
Code:
awk ' /^Application Name:/ { n=$NF }
/^Status: Running/ { print n } ' filename
|
|
1 members found this post helpful.
|
08-18-2016, 04:47 AM
|
#11
|
LQ Newbie
Registered: May 2016
Posts: 10
Original Poster
Rep: 
|
Thank you so much.
It worked.
awk ' /^Application Name:/ { n=$NF }
/Running/ { print n } ' output.txt
|
|
|
08-18-2016, 04:49 AM
|
#12
|
LQ Newbie
Registered: May 2016
Posts: 10
Original Poster
Rep: 
|
Solution
Quote:
Originally Posted by pan64
I would use awk instead of grep:
Code:
awk ' /^Application Name:/ { n=$NF }
/^Status: Running/ { print n } ' filename
|
thank you
|
|
|
08-18-2016, 07:45 AM
|
#13
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,576
|
You can also 'grep'.
Code:
grep -B5 "^Status: Running" filename | grep "^Application Name:"
|
|
|
08-18-2016, 08:41 AM
|
#14
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,042
|
As others have shown, awk is better suited to the problem, but bash is still fine. I would add that your solution from post #6 does not reflect what you intially told us:
Quote:
I am able to read file using while IFS and tried different awk combinations also.
|
None of the items highlighted in red appear anywhere in your solution. Furthermore, for loops should not be used to read a file.
Here is a possible bash solution:
Code:
#!/usr/bin/env bash
while IFS=":" read -r key value
do
[[ "$key" == "Application Name" ]] && keep="${value##* }"
[[ "$key" == "Status" && "${value##* }" == "Running" ]] && echo "$keep"
done<"$1"
As you can see, this does use 'while' and 'IFS' and has no need for something like awk
|
|
|
08-18-2016, 11:56 AM
|
#15
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,858
|
@linuxuser06, since the file is created by another script, shouldn't it possible to extract running application name from it and avoid doing a 2nd pass?
|
|
|
All times are GMT -5. The time now is 10:19 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|