Home > Blockchain >  Current Count vs Total Count output in a single line using Bash
Current Count vs Total Count output in a single line using Bash

Time:12-01

I need an output of current count vs total count in single line. I would like to know if this could be done Via Bash using 'for' 'while' loop.

Expecting an output that should only update the count and should not display multiple lines

#!/bin/sh

j=1
k=$(cat ~/test.rtf | wc -l)

for i in $(cat ~/test.rtf);
do
echo "Working on line ($j/$k)"
echo "$i" 
#or any other command for i

j=$((j 1))

done

EX:

Working on line (25/1267)

Not like,

Working on line (25/1267)
Working on line (26/1267)

CodePudding user response:

Something along these lines:

file=~/test.rtf
nl=$(wc -l "$file")
nl=${nl%%[[:blank:]]*}
i=0
while IFS= read -r line; do
    i=$((i 1))
    echo "Working on line ($i/$nl)"
done < "$file"

CodePudding user response:

With bash, what you have is sh. Something like:

#!/usr/bin/env bash

n=1
total_count="$(wc -l < ~/test.rft)"

while IFS= read -r line; do
  printf 'working on line (%d/%d)\n' $((n  )) "$total_count"
  printf '%s\n' "$line"
done < ~/test.rtf
  • See why you should not DRLWF , the shell is not Python or the likes.
  • Related