-1

I was trying the below script in terminal as well as script file,

while IFS= read -r line; do
    line_found=$(echo $line | grep "copied" && b_found=1 || b_found=0)
    echo $line_found
    str_source_1=$(echo $line_found | cut -d" " -f1)
    echo $str_source_1
    l_n_bytes_copied=$str_source_1
    echo $l_n_bytes_copied
done < progress_log.txt

But is giving the error,

0
8159232 bytes (8.2 MB) copied, 1.000737 s, 8.2 MB/s
8159232
8159232")syntax error: operand expected (error token is "

I am stuck on this error, also tried echo $line_found | awk '{print $1}' to extract the number from the file,

File contains

enter image description here

I need to get the first number of last line in the file,

In the script file,

declare -i l_n_bytes_copied=0;
line_found="";b_found=0;
while read line; 
do 
    line_found=$(echo $line | grep "copied" && b_found=1 || b_found=0);
    if [ $b_found == 1 ] | [ "$line_found" != "" ]; 
    then 
        str_source_1=$(echo $line_found | cut -d" " -f1);
        echo "---1      $str_source_1";
        l_n_bytes_copied=$str_source_1;
        #Another method tried :- Adding these for better understanding what I have done
        #l_n_bytes_copied=$(echo $line_found | awk '{print $1}')
        # sed is used to replace the ^M as \n, because input file is showing as one line file with seprator ^M file while opening with vim editor, this command has performed earlier before this script made the file as line line by line, 
        #sed -i -e 's/^M/\n/g' progress_log.txt
    fi;
done < p_log.txt ;
tripleee
  • 158,107
  • 27
  • 234
  • 292
Akhil V Suku
  • 870
  • 1
  • 12
  • 33
  • The symptoms look like you used a DOS editor on your script or the data file, but your code also has several quoting problems and random syntax errors etc. Try http://shellcheck.net/ for some diagnostics. – tripleee May 24 '22 at 07:17
  • 1
    Sounds like you are simply looking for `awk '/copied/ { n=$1 } END { print n }' p_log.txt` anyway. – tripleee May 24 '22 at 07:20
  • [shellcheck.net](https://www.shellcheck.net) will point out several other mistakes in the script; I recommend using it to scan for common mistakes. – Gordon Davisson May 24 '22 at 07:46

0 Answers0