Computer Hope

Software => BSD, Linux, and Unix => Topic started by: viceroy2528 on July 15, 2010, 11:20:55 AM

Title: Unix Script
Post by: viceroy2528 on July 15, 2010, 11:20:55 AM

Would anyone by chance have a Unix Script that would parse the output of a command for the required data and then issue another command with this data?
Title: Re: Unix Script
Post by: Geek-9pm on July 15, 2010, 04:09:48 PM
Take a look at
Unix Script Examples
http://UNIX.ittoolbox.com 
Title: Re: Unix Script
Post by: ghostdog74 on July 15, 2010, 08:36:48 PM
Would anyone by chance have a Unix Script that would parse the output of a command for the required data and then issue another command with this data?
what are you trying to parse? describe more clearly what you want to do.
Title: Re: Unix Script
Post by: viceroy2528 on July 16, 2010, 05:13:36 AM
Command
    tesmcmd jobmon +i -a 41844

Return
      <execute>
   <cmd_name>jobmon</cmd_name>
   <result>
     <jobrun>
       <job_run_id>3318780</job_run_id>
     </jobrun>
   </result>

I want to take the returned job_run_id and pass it to command

    tesmcmd jobrelease 3318780
Title: Re: Unix Script
Post by: ghostdog74 on July 16, 2010, 06:19:54 AM
Code: [Select]
   linux$ var=$(tesmcmd jobmon +i -a 41844 | awk -vRS="</job_run_id>" '/job_run_id/{gsub(/.*job_run_id>/,"");print}')
   linux$ tesmcmd jobrelease $var
Title: Re: Unix Script
Post by: KenJackson on July 17, 2010, 12:03:35 AM
Alternately,
Code: [Select]
var="$(tesmcmd jobmon +i -a 41844 | sed -n "/run_id/s/[^0-9]//gp")"
tesmcmd jobrelease $var

Some explanation:

var="$( ... )" captures the output to a variable,
-n tells sed to not output anything unless the p flag is given,
/run_id/ selects only the line that has that text,
s/...// is the substitution syntax, though we are substituting nothing thus deleting,
[^0-9] matches any non-numeric character,
g makes the substitution global on matching lines,
p means print the results.