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. |
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.
|
Are you interested in something like this:
Code:
$ grep 'Application Name\|^Status' sample.txt |
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.
|
Code:
#For list of files |
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 ======= |
what is $1 in your last post?
Please use [code]here comes your code[/code] to keep formatting |
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. |
$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 |
I would use awk instead of grep:
Code:
awk ' /^Application Name:/ { n=$NF } |
Thank you so much.
It worked. awk ' /^Application Name:/ { n=$NF } /Running/ { print n } ' output.txt |
Solution
Quote:
|
You can also 'grep'.
Code:
grep -B5 "^Status: Running" filename | grep "^Application Name:" |
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:
Here is a possible bash solution: Code:
#!/usr/bin/env bash |
@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 05:31 PM. |