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 08-17-2016, 06:59 AM   #1
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 10

Rep: Reputation: Disabled
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.
 
Old 08-17-2016, 07:46 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,976

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
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.
Old 08-17-2016, 01:22 PM   #3
cseanburns
Member
 
Registered: Nov 2003
Distribution: Fedora, Debian, Ubuntu, FreeBSD
Posts: 129

Rep: Reputation: 23
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.)
 
Old 08-17-2016, 01:29 PM   #4
notKlaatu
Senior Member
 
Registered: Sep 2010
Location: Lawrence, New Zealand
Distribution: Slackware
Posts: 1,077

Rep: Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731
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.
 
Old 08-17-2016, 04:45 PM   #5
teckk
Senior Member
 
Registered: Oct 2004
Distribution: Arch
Posts: 4,791
Blog Entries: 6

Rep: Reputation: 1711Reputation: 1711Reputation: 1711Reputation: 1711Reputation: 1711Reputation: 1711Reputation: 1711Reputation: 1711Reputation: 1711Reputation: 1711Reputation: 1711
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
 
Old 08-18-2016, 03:37 AM   #6
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
=======
 
Old 08-18-2016, 03:49 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,215

Rep: Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834
what is $1 in your last post?
Please use [code]here comes your code[/code] to keep formatting
 
Old 08-18-2016, 03:54 AM   #8
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-18-2016, 03:57 AM   #9
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
$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
 
Old 08-18-2016, 04:10 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,215

Rep: Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834
I would use awk instead of grep:
Code:
awk ' /^Application Name:/ { n=$NF }
      /^Status: Running/ { print n } ' filename
 
1 members found this post helpful.
Old 08-18-2016, 04:47 AM   #11
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thank you so much.

It worked.

awk ' /^Application Name:/ { n=$NF }
/Running/ { print n } ' output.txt
 
Old 08-18-2016, 04:49 AM   #12
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
Solution

Quote:
Originally Posted by pan64 View Post
I would use awk instead of grep:
Code:
awk ' /^Application Name:/ { n=$NF }
      /^Status: Running/ { print n } ' filename
thank you
 
Old 08-18-2016, 07:45 AM   #13
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,167

Rep: Reputation: 2644Reputation: 2644Reputation: 2644Reputation: 2644Reputation: 2644Reputation: 2644Reputation: 2644Reputation: 2644Reputation: 2644Reputation: 2644Reputation: 2644
You can also 'grep'.
Code:
grep -B5 "^Status: Running" filename | grep "^Application Name:"
 
Old 08-18-2016, 08:41 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,976

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
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
 
Old 08-18-2016, 11:56 AM   #15
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,541

Rep: Reputation: 866Reputation: 866Reputation: 866Reputation: 866Reputation: 866Reputation: 866Reputation: 866
@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?
 
  


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
Read .log format file and get special character from some lines by shell scripting samasara Linux - Newbie 32 12-21-2013 02:17 AM
shell scripting for extracting input from different directorires kswapnadevi Linux - Newbie 7 11-09-2010 06:10 AM
deleting particular lines using shell scripting ibabhelix Linux - Newbie 9 11-02-2009 12:26 AM
extracting particular lines from a text file skuz_ball Programming 18 10-28-2008 12:31 PM
Inserting lines into a file through shell scripting false-hopes Linux - General 1 10-22-2005 11:39 AM

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

All times are GMT -5. The time now is 04:01 PM.

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