-1

I have a text file which has only integer values i.e different integers in different lines(say from 1 to 47). I have made a script that would read each line and take the value present in a different line. If the condition is met, then I want to echo a statement.

contents of a.txt: (In one line there is only 1, In second line there is only 2, as is) 1 2 3 4 5 ..so on till 47.

Output I want: As soon it reads 5. Output - "Step Completed is 5" (without double quotes) . This should go for 5,10,15,20 till 45

Here is the code but it doesn't seem to work.

 #!/bin/bash

while IFS= read -r line; do
        if [[ $line=="5" ]] ; then
           echo "Step Completed is:" $var
        fi
done < "$1"

Also I want to echo the same statement for every 5 integer values i.e as soon as the script reads 5, it should echo - Step completed is 5. As soon as it reads 10, it should echo - Step Completed is 10. Like this.

To run the script I am using the command:

. ./al.sh a.txt
  • Can you give an example of the input file and the desired output? – chaos Aug 30 '18 at 8:20
  • a.txt: (In one line there is only 1, In second line there is only 2, as is) 1 2 3 4 5 ..so on till 47. Output I want: As soon it reads 5. Output - "Step Completed is 5" (without double quotes) . This should go for 5,10,15,20 till 45 – Tanmay Aug 30 '18 at 8:23
  • @Tanmay can you edit the question and add the content you have given in the comment. – msp9011 Aug 30 '18 at 8:38
0

Instead of doing this in a shell script, I'd suggest doing it with awk:

awk '$1 % 5 == 0 { last_step = $1 } END { printf("Step %s completed\n", last_step ) }' <a.txt

This keeps the code short, fast, and simple. The awk code stores each multiple of 5 that it finds in its input, and at the end it prints the most recent such number found.

Testing (in a shell that has brace expansions):

$ printf '%s\n' {1..47} | awk '$1 % 5 == 0 { last_step = $1 } END { printf("Step %s completed\n", last_step ) }'
Step 45 completed

The equivalent shell loop would look like

while read number; do
    if [ "$(( number % 5 ))" -eq 0 ]; then
        last_step=$number
    fi
done <a.txt

printf 'Step %s completed\n' "$last_step"

You may want to insert a test to make sure that last_step has a valid value before doing any output, or you will get bogus output if your input contains no multiples of five.

  • I want it in such a way that if 10 is found so it shouldn't print - Step Completed : 5. If 20 is found, it shouldn't print 5,10,15. Just print the last line which on modulus with 5 gives 0. – Tanmay Aug 30 '18 at 11:00
  • @Tanmay Ah, this was not clear from the question. Just a moment... There, fixed it. – Kusalananda Aug 30 '18 at 11:00
  • Thanks, that works like a charm. Sorry for keeping some part of the question ambiguous. – Tanmay Aug 30 '18 at 11:10
0

If you want to output a "step completed" everything 5 lines, then takes the rest of the step number when divided by 5 and compare to 0:

#!/bin/bash

while IFS= read -r line; 
do
        if [[ $(( line %5 )) -eq  0 ]] ; then
        echo "Step Completed is: $line"
        fi
done < "$1"

(not also that in your script you variable is called line or var....)

If you want only the last such line, then you have to wait until the end to print it. If you have a complete sequence of number then you can compute it at the end:

#!/bin/bash

while IFS= read -r line; 
do
    # do whatever for the line
    echo $line >/dev/null
done < "$1"
echo "Step completed is: $(( 5 * ( line % 5 ) ))"

Otherwise just remember the last on you saw:

#!/bin/bash

while IFS= read -r line; 
do
    [[ $(( line %5 )) -eq  0 ]] && line5=$line 
    # do whatever for the line
done < "$1"
[[ ! -z "$line5" ]]  && echo "Step completed is: $line5"
  • Since he's sourcing the script, the #!-line is not needed (unless he later wants to run it in the more ordinary way). – Kusalananda Aug 30 '18 at 8:40
  • if (( line % 3 == 0 )); then should also work, i.e. you can do the comparison in the arithmetic expansion too. For completeness' sake, you could add a check to see if the line read is numeric. – ilkkachu Aug 30 '18 at 9:22
  • I want it in such a way that if 10 is found so it shouldn't print - Step Completed : 5. If 20 is found, it shouldn't print 5,10,15. Just print the last line which on modulus with 5 gives 0. – Tanmay Aug 30 '18 at 9:52
  • See edited answer. – xenoid Aug 30 '18 at 12:14

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.