LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 07:59 AM   #1
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 8

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, 08:46 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 02:22 PM   #3
cseanburns
Member
 
Registered: Nov 2003
Distribution: Debian, Ubuntu, FreeBSD
Posts: 118

Rep: Reputation: 18
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, 02:29 PM   #4
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware
Posts: 893

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
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, 05:45 PM   #5
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 1,825

Rep: Reputation: 221Reputation: 221Reputation: 221
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, 04:37 AM   #6
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 8

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, 04:49 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

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

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, 04:57 AM   #9
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 8

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, 05:10 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
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, 05:47 AM   #11
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 8

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, 05:49 AM   #12
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 8

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, 08:45 AM   #13
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,430

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

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 12:56 PM   #15
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
@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 03:17 AM
shell scripting for extracting input from different directorires kswapnadevi Linux - Newbie 7 11-09-2010 07:10 AM
deleting particular lines using shell scripting ibabhelix Linux - Newbie 9 11-02-2009 01:26 AM
extracting particular lines from a text file skuz_ball Programming 18 10-28-2008 01:31 PM
Inserting lines into a file through shell scripting false-hopes Linux - General 1 10-22-2005 12:39 PM


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