Loop over variable content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop over variable content
# 1  
Old 08-03-2011
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:

Code:
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:

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

Any ideas guys on how I can do this
# 2  
Old 08-03-2011
With bash:
Code:
$ 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:
Code:
echo "$a" | while read line; do
  echo "$line"
done

# 3  
Old 08-03-2011
yazu, thanks for your reply

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

Quote:
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
So it is not enough for me just to loop all over the lines, but only those having my pattern

Any suggestion?
# 4  
Old 08-03-2011
Code:
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

# 5  
Old 08-03-2011
Here is a pure shell solution:
Code:
#!/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:
Code:
XXXXXXX
YYYYYY
ZZZZZZ

# 6  
Old 08-04-2011
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:

Code:
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:

Code:
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!

Code:
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Getting ls content into a file using variable

hi i just cant figure out how can i do this ls -lt > log.txt using $PWD what i mean is how can i get the ls command content into a file using $PWD variable? :confused: (4 Replies)
Discussion started by: chinababy
4 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

5. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

6. Shell Programming and Scripting

how to get the specified content of the text into a variable.

I am having one string like ./usr1/Server/temp/app.env ./usr1/Server/temp/upp/app.env ./usr1/Server/ORIG_temp/app.env ./usr1/Server/ORIG_temp/upp/app.env ./usr1/Server/work_temp_40/app.env ./usr1/Server/work_temp_40/upp/app.env ./usr1/fd/app.env ./usr1/PurgeArchive/app.env ./usr1/bm/bin/app.env... (6 Replies)
Discussion started by: dineshmurs
6 Replies

7. Shell Programming and Scripting

addressing variable content...

I want to address a variable content whose name is/matches the content of a given other variable. i.e. set name=´sam´ set ${name}_age=´27´ So, by typing: echo ${name}_age I correctly obtain: sam_age By typing: echo $sam_age or echo ${sam_age} I correctly obtain: 27 But how can I... (3 Replies)
Discussion started by: sobolev
3 Replies

8. Shell Programming and Scripting

Content of variable

I have a variable that contains filenames like this: variable="file_1.extension<blank>file_2.extension<blank>file_3.extension<blank>file_4.extension and so on" How can I make filenames to be separated by newline: (I tried Sed but it didn't worked well) file_1.extension file_2.extension... (6 Replies)
Discussion started by: MartyIX
6 Replies

9. Shell Programming and Scripting

Content of Content of a variable!

I got a sample BASH script like this : $ cat test MYVAR=$1 DUMMY1="This is tricky" DUMMY2=24 echo $ $ ./test DUMMY1 ./test: line 5: This is tricky: syntax error in expression (error token is "is tricky") **I was expecting the output as "This is tricky", ah! but no luck **But... (2 Replies)
Discussion started by: jaduks
2 Replies

10. Shell Programming and Scripting

How to replace a variable content

Hi, variable1="This is a car" Now I want to replace the content of variable1, "car" to "dog". Is there any simple command I can use. Thanks. Joseph (4 Replies)
Discussion started by: josephwong
4 Replies
Login or Register to Ask a Question