Help removing output from .sh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help removing output from .sh script
# 1  
Old 08-09-2013
Help removing output from .sh script

I have the following script
Code:
#!/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: -

Code:
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: -

Code:
432 KB old_scripts
72 KB deploy_file_mar_2007_Pilot.sh

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

Smilie

Last edited by jim mcnamara; 08-09-2013 at 12:21 PM..
# 2  
Old 08-09-2013
Hello,

Just try this please.


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

Output will be as follows.

Code:
432 KB  old_scripts
72 KB deploy_file_mar_2007_Pilot.sh
$


Thanks,
R. Singh
# 3  
Old 08-09-2013
Works in recent shells:
Code:
printf "%s\n" ${ENTRY#*/}
deploy_file_mar_2007_Pilot.sh

man bash:
Quote:
Parameter Expansion
. . .
${parameter#word}
${parameter##word}
Remove matching prefix pattern. . . .
# 4  
Old 08-09-2013
Excellent I used the ${ENTRY#*/} and it works not 100% how though?

It now looks like: -
Code:
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


Last edited by Scott; 08-09-2013 at 01:18 PM.. Reason: Added code tags [5th time]; indented code
# 5  
Old 08-11-2013
Why does du -sk * not work?
# 6  
Old 08-12-2013
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
# 7  
Old 08-12-2013
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
Code:
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


Last edited by Jotne; 08-12-2013 at 10:26 AM.. Reason: fixed my own typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing characters and some output

Hi guys, So I am using an awk command to return a specific column in a file. I also need to remove some extra characters. What I have is :: (http-/0.0.0.0:8091-9)|23:00:41 And what I want is : http-/0.0.0.0:8091-9 I tried using the td command but it is only removing the ( ,... (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

2. Shell Programming and Scripting

Help in removing control M and Line feed in output file.

Hi All, In my output file i am getting control m character and also the line feeds at different places and with different combinations, the content of the file is supposed to be in a single line but if there is a line feed in between then from there onwards it's going into new line. I tried... (7 Replies)
Discussion started by: Bipin Kumar
7 Replies

3. UNIX for Dummies Questions & Answers

Removing unnecessary eol ($) character from Oracle sql query output

Hi All, I am fetching oracle query result in shell variable. As columns numbers are more the output wraps in unix terminal .i.e one complete record in db gets store in multiple lines. with each line ends with $ character. I want to remove these unnecessary $ character but to keep required $... (8 Replies)
Discussion started by: Harshal22
8 Replies

4. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

5. Shell Programming and Scripting

Removing Characters From finger Output

Hi Does anyone know of a command that will extract only the Login name alison and year 2005 from the following output. jamesm> finger alison Login name: alison In real life: Alison Smith - Queensland Directory: /data/home/alison Shell: /usr/bin/ksh... (2 Replies)
Discussion started by: jamba1
2 Replies

6. Shell Programming and Scripting

Removing spaces from the output of a head command

Im running the below commands in a script. Total_confirms=`cat $OUTPATH/count_po* | head -4 | tail -1` # echo "Total Confirms records we need: $Total_confirms" >> $LOG2 The problem is its always returning 4 spaces before the result.. Can I pipe the result into something else to remove the... (5 Replies)
Discussion started by: Jazmania
5 Replies

7. Shell Programming and Scripting

help removing characters for output file in shell script

hi i'm new to shell scripts and have a small problem i am running a batch converter that returns all flash .flv files in a directory and create a png image from each one the problem i have is the $1 variable , its ok on the first call but on the secound call $1.png , i have extra... (1 Reply)
Discussion started by: wingchun22
1 Replies

8. Shell Programming and Scripting

Removing Null data in output

Hello all, I have a script that has an infile with system package information. For the most part the script is looking well. The only thing i need help is in testing for null entries and removing null data. #!/usr/bin/ksh for i in `cat /mwncps/bin/eco_pack` do NAME=`pkginfo -l |... (2 Replies)
Discussion started by: liketheshell
2 Replies

9. Shell Programming and Scripting

Removing Garbage output

I am using following code to read myfile.ddl line by line. But the thing is it is printing lot of garbage which are the names of the files and directories in which myfile.ddl is present. Kindly refine the code so that only myfile.ddl contents are only read LOGFILE="logfile.txt"... (4 Replies)
Discussion started by: skyineyes
4 Replies

10. Shell Programming and Scripting

Removing first line from output

my script gives 10 outputs continuously..In each output i have to remove the first line in the output.How to do that. for eg : below is my output 0.00 1.00 5.00 0.00 7.00 i have to remove the first line of this output ie;0.00 (3 Replies)
Discussion started by: Krrishv
3 Replies
Login or Register to Ask a Question