Loop over variable content

Hello all,

I do have a variable containing one line like this:

Waiting for job XXXXXX to start

I needed to get the 'XXXXXX' literal, so I did the following:

job_interno=`echo $log_exec | sed 's/.*Waiting for job \([^ to start]*\).*/\1/' `
#other stuff
 

Now, my variable is have more than line like that:

Waiting for job XXXXXX to start
Waiting for job YYYYYY to start

Now, I need to loop over all these lines, get the job name (XXXXXX, YYYYYY...) and execute what is in #other stuff for every single line, but I dont know how

The furthest I got is to get how many times I do have to iterate:

numtimes=`echo $log_exec | grep "Waiting for job" | grep "to start" | wc -l

Any ideas guys on how I can do this

With bash:

$ cat script.sh
a='Waiting for job YYYYYY to start
Waiting for job XXXXXX to start'

while read line; do
  echo "$line"
done <<<"$a"

$ bash script.sh
Waiting for job YYYYYY to start
Waiting for job XXXXXX to start

With /bin/sh:

echo "$a" | while read line; do
  echo "$line"
done

yazu, thanks for your reply

Unfortunately, my input is not having all lines with same format, moreover, like this

So it is not enough for me just to loop all over the lines, but only those having my pattern

Any suggestion?

a='LINE HELLO
Waiting for job XXXXXXX to start
LINE HOW ARE YOU DOING
LINE FINE
Waiting for job YYYYYY to start
Waiting for job ZZZZZZ to start
MORE LINES'

echo "$a" | while read line; do
  if echo "$line" | grep -q '^Waiting'; then
      echo "do something with '$line'"
  fi
done 

Here is a pure shell solution:

#!/bin/bash

while read a b c d e f
do
    if [[ "$a $b $c" == "Waiting for job"  && "$e $f" == "to start" ]]
    then
        echo "$d"
    fi
done < file

Using you example file, the output is:

XXXXXXX
YYYYYY
ZZZZZZ

Hello guys,

Thanks very much for your replies, now I feel I am closer

I tried what you suggested, but it seems that I am losing lines when retrieving the information, let me explain:

My script is doing the following:

log_exec=`$path_ds_ex/dsjob -logdetail $proyecto $job`
#(doing this because need to use this many times later on)
 
numtimes=`echo $log_exec | grep "Waiting for job" | grep "to start" | wc -l `
echo $numtimes

Result for this is: 1

Otherwise, if I do this:

log_exec=`$path_ds_ex/dsjob -logdetail $proyecto $job`
 
numtimes=`$path_ds_ex/dsjob -logdetail $proyecto $job | grep "Waiting for job" | grep "to start" | wc -l `
echo $numtimes

Now result is correct: 24

On the other hand, I tried the code fpmurphy suggested, but got nothing at all!

echo "$log_exec" | while read a b c d e f; do
  if [[ "$a $b $c" == "Waiting for job"  && "$e $f" == "to start" ]]
    then
        echo "$d"
    fi
done

Can you please guys advice?