2

Have this tiny script down below and want to print out to the screen every other result. What are the ways of doing it?

#!/bin/bash

for ((n=5, j=1; j<=9;j++))
do
echo "rez: $n*$j"=$((n*j))

done
  • 1
    Do you mean something like for ((n=5, j=1; j<=9;j+=2))? – Hauke Laging Jan 7 '15 at 21:49
  • 3
    What monitor? What does it have to do with printf? – Stéphane Chazelas Jan 7 '15 at 21:51
  • Sorry I did not put myself clear. The results comes out as a column on the screen as follows: rez:5*1=5 rez:5*2=10 rez:5*3=15 and so on. I need to display every other line. e.g rez:5*1=5 then empty line rez:5*3=15 empty line and so on. – Kestutis Jan 7 '15 at 22:03
3

You could use sed to filter the output (of any command/script) and print every other line and double space the final output:
print only odd-numbered lines:

command | sed n\;g

print only even-numbered lines:

command | sed g\;n

command can be for ... done

1
#! /bin/bash

for ((n=5, j=1; j<=9;j++)); do
    if [ $((j%2)) -eq 1 ]; then
        echo "rez: $n*$j"=$((n*j))
    else
        echo
    fi
done

rez: 5*1=5

rez: 5*3=15

rez: 5*5=25

rez: 5*7=35

rez: 5*9=45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.