LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Extracting lines from a file using shell scripting (https://www.linuxquestions.org/questions/linux-newbie-8/extracting-lines-from-a-file-using-shell-scripting-4175587264/)

linuxuser06 08-17-2016 06:59 AM

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.

grail 08-17-2016 07:46 AM

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.

cseanburns 08-17-2016 01:22 PM

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

notKlaatu 08-17-2016 01:29 PM

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.

teckk 08-17-2016 04:45 PM

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


linuxuser06 08-18-2016 03:37 AM

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
=======

pan64 08-18-2016 03:49 AM

what is $1 in your last post?
Please use [code]here comes your code[/code] to keep formatting

linuxuser06 08-18-2016 03:54 AM

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.

linuxuser06 08-18-2016 03:57 AM

$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

pan64 08-18-2016 04:10 AM

I would use awk instead of grep:
Code:

awk ' /^Application Name:/ { n=$NF }
      /^Status: Running/ { print n } ' filename


linuxuser06 08-18-2016 04:47 AM

Thank you so much.

It worked.

awk ' /^Application Name:/ { n=$NF }
/Running/ { print n } ' output.txt

linuxuser06 08-18-2016 04:49 AM

Solution
 
Quote:

Originally Posted by pan64 (Post 5592422)
I would use awk instead of grep:
Code:

awk ' /^Application Name:/ { n=$NF }
      /^Status: Running/ { print n } ' filename


thank you

allend 08-18-2016 07:45 AM

You can also 'grep'.
Code:

grep -B5 "^Status: Running" filename | grep "^Application Name:"

grail 08-18-2016 08:41 AM

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

keefaz 08-18-2016 11:56 AM

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