-1

I am looping our the a grep result. The result contains 10 lines (every line has different content). So the loop stuff in the loop gets executed 10 times.

I need to get the index, 0-9, in the run so i can do actions based on the index.

ABC=(cat test.log | grep "stuff")

counter=0
for x in $ABC
do
        echo $x      
        ((counter++))
        echo "COUNTER $counter"
done

Currently the counter won't really change.

Output:

51209
120049
148480
1211441
373948
0
0
0
728304
0
COUNTER: 1
shelly
  • 309
  • 3
  • 14
  • 1
    `i can do actions based on the index`: What action do you want to do? Your output doesn't like coming from the script shown. Your `echo` command has a `:` which is not appearing in output. – anubhava Jan 08 '21 at 06:05
  • Is `ABS` a variable, or an array? – Ivan Jan 08 '21 at 06:57

4 Answers4

2

If your requirement is to only print counter(which is as per shown samples only), in that case you could use awk(if you are ok with it), this could be done in a single awk like, without creating variable and then using grep like you are doing currently, awk could perform both search and counter printing in a single shot.

awk -v counter=0 '/stuff/{print "counter=" counter++}' Input_file

Replace stuff string above with the actual string you are looking for and place your actual file name for Input_file in above.

This should print like:

counter=1
counter=2
........and so on
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1

Your shell script contains what should be an obvious syntax error.

ABC=(cat test.log | grep "stuff")

This fails with

-bash: syntax error near unexpected token `|'

There is no need to save the output in a variable if you only want to process one at a time (and obviously no need for the useless cat).

grep "stuff" test.log | nl

gets you numbered lines, though the index will be 1-based, not zero-based.

If you absolutely need zero-based, refactoring to Awk should solve it easily:

awk '/stuff/ { print n++, $0 }' test.log

If you want to loop over this and do something more with this information,

awk '/stuff/ { print n++, $0 }' test.log |
while read -r index output; do
    echo index is "$index"
    echo output is "$output"
done

Because the while loop executes in a subshell the value of index will not be visible outside of the loop. (I guess that's what your real code did with the counter as well. I don't think that part of the code you posted will repro either.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
1

Do not store the result of grep in a scalar variable $ABC. If the line of the log file contains whitespaces, the variable $x is split on them due to the word splitting of bash. (BTW the statement ABC=(cat test.log | grep "stuff") causes a syntax error.)

Please try something like:

readarray -t abc < <(grep "stuff" test.log)

for x in "${abc[@]}"
do
    echo "$x"
    echo "COUNTER $((++counter))"
done

or

readarray -t abc < <(grep "stuff" test.log)

for i in "${!abc[@]}"
do
    echo "${abc[i]}"
    echo "COUNTER $((i + 1))"
done
tshiono
  • 21,248
  • 2
  • 14
  • 22
0

you can use below increment statement-

counter=$(( $counter + 1));
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Ajit Kumar
  • 11
  • 1