Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is one suggested approach. It wasn't clear from the sample you posted, so I am assuming that this is actually what your sample file looks like:</p> <pre><code>UNIX_PID 37165 PARTNER_ID prad XML_FILE /mnt/publish/gbl/backup/pradeep1/27241-20090722/kumarelec2.xml pradeep1 /mnt/soar_publish/gbl/backup/pradeep1/11089-20090723/dataonly.xml UNIX_PID 27654 PARTNER_ID swam XML_FILE smariswam2 /mnt/publish/gbl/backup/smariswam2/10235-20090929/swam2.xml </code></pre> <p>I am also assuming that:</p> <ol> <li>There is a line-feed at the end of the last line of your file.</li> <li>The columns are separated by a single space.</li> </ol> <p>Here is a suggested bash script (not optimal, I'm sure, but functional):</p> <pre><code>#! /bin/bash cat myOutputData.txt | while read line; do myPID=`echo $line | awk '{print $2}'` isRunning=`ps -p $myPID | grep $myPID` if [ -n "$isRunning" ] then echo "PARTNER_ID `echo $line | awk '{print $4}'`" echo "XML_FILE `echo $line | awk '{print $6}'`" fi done </code></pre> <p>The script iterates through every line (row) of the input file. It uses awk to extract column 2 (the PID), and then does a check (using ps -p) to see if the process is running. If it is, it uses awk again to pull out and echo two fields from the file (PARTNER ID and XML FILE). You should be able to adapt the script further to suit your needs. Read up on awk if you want to use different column delimiters or do additional text processing.</p> <p>Things get a little more tricky if the output file contains one row for each data element (as you indicated). A good approach here is to use a simple state mechanism within the script and "remember" whether or not the most recently seen PID is running. If it is, then any data elements that appear before the next PID should be printed out. Here is a commented script to do just that with a file of the format you provided. Note that you must have a line-feed at the end of the last line of input data or the last line will be dropped.</p> <pre><code>#! /bin/bash cat myOutputData.txt | while read line; do # Extract the first (myKey) and second (myValue) words from the input line myKey=`echo $line | awk '{print $1}'` myValue=`echo $line | awk '{print $2}'` # Take action based on the type of line this is case "$myKey" in "UNIX_PID") # Determine whether the specified PID is running isRunning=`ps -p $myValue | grep $myValue` ;; "PARTNER_ID") # Print the specified partner ID if the PID is running if [ -n "$isRunning" ] then echo "PARTNER_ID $myValue" fi ;; *) # Check to see if this line represents a file name, and print it # if the PID is running inputLineLength=${#line} if (( $inputLineLength &gt; 0 )) &amp;&amp; [ "$line" != "XML_FILE" ] &amp;&amp; [ -n "$isRunning" ] then isHyphens=`expr "$line" : -` if [ "$isHyphens" -ne "1" ] then echo "XML_FILE $line" fi fi ;; esac done </code></pre> <p>I think that we are well into custom software development territory now so I will leave it at that. You should have enough here to customize the script to your liking. Good luck!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload