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

TenTenths 08-19-2016 02:03 AM

I came up with a pure bash solution because.... I wanted to... :)

Code:

#!/bin/bash

while IFS=":" read -r item value ; do

  if [[ ${item} == "Application Name" ]] ; then
    myapp=${value}
  fi

  if [[ ${item} == "Status" ]] && [ ${value} == "Running" ] ; then
      echo "${myapp} is running"
      myapp=""
  fi

done < test.dat

This also takes in to consideration additions or subtractions from the file, as long as the Application Name: line is before the Status: line it should work, even if other lines are added or subtracted.

In reality I'd have used the awk suggestion, but just wanted to see if I could do it purely in bash.

edit: Just noticed this is pretty similar to grail's solution (post #14)

linuxuser06 12-06-2016 02:43 AM

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


Hey

This was working fine for most of the cases. But recently we got a test case like below and it did not work accurately.

Sample data in file
<<Application Name: abc def_ghi/def_ghi/def_ghi
Service Name: abc_def_ghi.par
Deployment Status: Success
Service Instance Name: abc_def_ghi
Machine Name: abc00a0194
Status: Running>>

On running below code it is giving output as "def_ghi/def_ghi/def_ghi" rather than "abc def_ghi/def_ghi/def_ghi"
Code:


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

Can you please suggest some modification in this awk command so that it displays the complete application name including the space?

thanks in advance.

pan64 12-06-2016 02:46 AM

probably awk -F: '...... ' output.txt will help

avijadhav 12-06-2016 02:49 AM

hey friends

how to update 6.3 to 6.11 Redhat server online and offline please give me step

pan64 12-06-2016 02:59 AM

please do not hide threads, open a new one if you need help (from the other side please ask Redhat)

linuxuser06 12-06-2016 03:48 AM

Quote:

Originally Posted by pan64 (Post 5638539)
probably awk -F: '...... ' output.txt will help

that worked!! cool thanks!!

rtmistler 12-06-2016 12:50 PM

Quote:

Originally Posted by avijadhav (Post 5638540)
hey friends

how to update 6.3 to 6.11 Redhat server online and offline please give me step

avijadhay,

Do not crosspost or hijack other threads, and keep this same question within the original thread you created on this subject. Meanwhile fix your question in that thread to be able to obtain better assistance on this matter.

Persisting with this type of behavior will not be helpful to you or the LQ forum.


All times are GMT -5. The time now is 04:20 AM.