substr issue

Hi

I have a script:

 
ls -l | grep "$TDATE" | awk '{print $NF}' > todays_files.txt
for run in $(cat todays_files.txt)
do
 SSTR='expr substr "$run" 1 5'
 echo "$SSTR"
done
 

I want 1 to 5 chars of each files.

It returns instead for all files.

'expr substr "$run" 1 5'

Could someone help me to fix the issue?

Thanks

a="abcdefg"
echo ${a:0:5}

output:
abcde

Try this instead of the loop:

ls -l | awk -v d="$TDATE" '$0 ~ d{print substr($NF,1,5)}'
1 Like

Thanks Franklin & Icon.

You can also pipe the output from the awk to a code block { } around your loop and avoid writing to a file.

 
ls -l | grep "$TDATE" | awk '{print $NF}' |
{ while read run; do 
     echo ${run:0:5}
done } 

read run not only reads the line but also evaluates to FALSE when it can't read the next to last line breaking while the look at the top.