Help removing output from .sh script

I have the following script

#!/bin/ksh
# **********************************************************************
#
# System:       xxxx
#
# Filename:     List_Largest_Files.sh
#
# Purpose:      List 10 largest files in current partition
#
# Modification History:
# 1.0                    Initial Version
# ************************************************************************
du -sk ./* | sort -rn | head | \
while read SIZE ENTRY
do
# if size > 1048576 then it is at least 1 GB big
if [ ${SIZE} -gt 1048576 ]
then
NEWSIZE=`echo "${SIZE}000 / 1048576" | bc | sed -e "s/\(...\)$/\.\1/"`
printf "% 10s %s\n" "${NEWSIZE} GB" $ENTRY
# if size > 1024 then it is at least 1 MB big
elif [ ${SIZE} -gt 1024 ]
then
NEWSIZE=`echo "${SIZE}000 / 1024" | bc | sed -e "s/\(...\)$/\.\1/"`
printf "% 10s %s\n" "${NEWSIZE} MB" $ENTRY
else
printf "% 10s %s\n" "${SIZE} KB" $ENTRY
fi
done

The output is as follows: -

432 KB  ./old_scripts
72 KB ./deploy_file_mar_2007_Pilot.sh

Is there a way to remove the ./ from the output so it looks like: -

432 KB old_scripts
72 KB deploy_file_mar_2007_Pilot.sh

I have tried du -sk * but this doesn't work

:wall:

Hello,

Just try this please.

$ ksh script name | sed 's/\.\///g' remove_dot_slash

Output will be as follows.

432 KB  old_scripts
72 KB deploy_file_mar_2007_Pilot.sh
$

Thanks,
R. Singh

Works in recent shells:

printf "%s\n" ${ENTRY#*/}
deploy_file_mar_2007_Pilot.sh

man bash :

Excellent I used the ${ENTRY#*/} and it works not 100% how though?

It now looks like: -

du -sk ./* | sort -rn | head | \
while read SIZE ENTRY
do
  # if size > 1048576 then it is at least 1 GB big
  if [ ${SIZE} -gt 1048576 ]
  then
    NEWSIZE=`echo "${SIZE}000 / 1048576" | bc | sed -e "s/\(...\)$/\.\1/"`
    printf "% 10s %s\n" "${NEWSIZE} GB" ${ENTRY#*/}
    # if size > 1024 then it is at least 1 MB big
  elif [ ${SIZE} -gt 1024 ]
  then
    NEWSIZE=`echo "${SIZE}000 / 1024" | bc | sed -e "s/\(...\)$/\.\1/"`
    printf "% 10s %s\n" "${NEWSIZE} MB" ${ENTRY#*/}
  else
    printf "% 10s %s\n" "${SIZE} KB" ${ENTRY#*/}
  fi
done

Why does du -sk * not work?

According to my colleague its the * that caused the problem. Apparently it doesn't know what directory to look in if you use du -sk * so I had to use du -sk ./*

Its a HPUX file system

I do not understand why you add the three 000 to the size?
Also get rid of back tics `` and use parentheses $()
BC is not default installed on Ubuntu do the math in shell $((a+b)) or awk if you need decimal.

Here is some rewritten version

du -sk ./* | sort -rn | head | \
while read SIZE ENTRY
do
  # if size > 1048576 then it is at least 1 GB big
  if [ ${SIZE} -gt 1048576 ]
  then
    NEWSIZE=$((${SIZE} / 1048576))
    printf "% 10s %s\n" "${NEWSIZE} GB" ${ENTRY#*/}
    # if size > 1024 then it is at least 1 MB big
  elif [ ${SIZE} -gt 1024 ]
  then
    NEWSIZE=$((${SIZE} / 1024))
    printf "% 10s %s\n" "${NEWSIZE} MB" ${ENTRY#*/}
  else
    printf "% 10s %s\n" "${SIZE} KB" ${ENTRY#*/}
  fi
done

Your colleague told you nonsense!

du -sk *

does the same as

du -sk ./*

but reports the plain file names instead of prefixing them with ./ .
And tell him you are running du -sk ././* for double safety:D

Some variation using awk

du -sk * | sort -rn | awk '{if ($1>1048576) {$1=$1/1048576;t="GB"} else if ($1>1024) {$1=$1/1024;t="MB"} else {t="KB"}} NR < 11 {printf "% 10s %s %s\n",$1,t,$2}'
du -sk * | sort -rn | awk '{
	if ($1>1048576) {$1=$1/1048576;t="GB"} 
	else if ($1>1024) {$1=$1/1024;t="MB"} 
	else {t="KB"}
	} 
	NR < 11 {printf "% 10s %s %s\n",$1,t,$2}'

With two efficiency tricks and extended range it becomes

du -sk * | sort -rnk1 | awk '
NR>10 {exit}
{
  if ($1>1073741824) {$1=$1/1073741824; t="TB"}
  else if ($1>1048576) {$1=$1/1048576; t="GB"}
  else if ($1>1024) {$1=$1/1024; t="MB"}
  else {t="KB"}
}
{printf "%10s %s %s\n",$1,t,$2}
'