Shell script to print date and no.of records

hi all,
i want script to print the output in the following format

    filename yyyy/mm/dd count

where count= no.of records in the file

Thanks in advance
hemanthsaikumar

for file in *
do
  count=$(wc -l < $file)
  echo $file `date +%Y/%m/%d` $count
done
1 Like

hey thankyou very much for your help
one more doubt
i want to restrict the filename to 30 bytes.. that means if the file name is 20 bytes it should print the 30 byte name by taking the space as input

Thanks in advance
saikumar

for file in *
do
  count=$(wc -l < $file)
  #echo $file `date +%Y/%m/%d` $count
   printf "%-30s %10s %5d\n " $file `date +%Y/%m/%d` $count
done

if the file name length is less than 30 bytes the spaces are getting appended...
what if the file name is greater than 30 bytes....?
for the above code it is not getting trimmed to 30 bytes....

thanks in advance
saikumar.

for file in *
do
  COUNT=$(wc -l < $file)
  DATE=$(date +%Y/%m/%d)
  file=${file:0:30}
  #echo $file `date +%Y/%m/%d` $count
   printf "%-30s %10s %5d\n " $file $DATE $COUNT
done

could you please explain what is
file=${file:0:30}
%-30s %10s %5d\n

go through the ABS (advanced bash scripting guide), you should get all the answers.

http://www.google.com.au/\#q=advanced\+bash\+scripting\+guide\+pdf&hl=en&tbo=d&biw=1280&bih=939&fp=1&bav=on.2,or.r\_gc.r\_pw.r_qf.&cad=b

1 Like

thank u very much for your time being help... :slight_smile:

Also in this forum, has "man page" already:

Man Page for printf (linux Section 1) - The UNIX and Linux Forums

Man Page for bash (linux Section 1) - The UNIX and Linux Forums

      ${parameter:offset}
       ${parameter:offset:length}
	      Substring Expansion.  Expands to	up  to	length	characters  of
	      parameter  starting  at  the  character specified by offset.  If
	      length is omitted, expands to the substring of parameter	start-
	      ing at the character specified by offset.  length and offset are
	      arithmetic expressions (see ARITHMETIC  EVALUATION  below).   If
	      offset  evaluates  to a number less than zero, the value is used
	      as an offset from the end of the value of parameter.  Arithmetic
	      expressions  starting  with  a - must be separated by whitespace
	      from the preceding : to be distinguished from  the  Use  Default
	      Values  expansion.   If  length  evaluates to a number less than
	      zero, and parameter is not @ and not an indexed  or  associative
	      array,  it is interpreted as an offset from the end of the value
	      of parameter rather than a number of characters, and the	expan-
	      sion is the characters between the two offsets.  If parameter is
	      @, the result is length positional parameters beginning at  off-
	      set.   If parameter is an indexed array name subscripted by @ or
	      *, the result is the length members of the array beginning  with
	      ${parameter[offset]}.   A  negative  offset is taken relative to
	      one greater than the maximum index of the specified array.  Sub-
	      string  expansion applied to an associative array produces unde-
	      fined results.  Note that a negative offset  must  be  separated
	      from  the  colon	by  at least one space to avoid being confused
	      with the :- expansion.  Substring indexing is zero-based	unless
	      the  positional  parameters are used, in which case the indexing
	      starts at 1 by default.  If offset  is  0,  and  the  positional
	      parameters are used, $0 is prefixed to the list.

hi,
by the above code while trimming the length of the filename to 30 bytes ,the extension is also getting trimmed :frowning:

like if the filename is abscderss.txt(assume as its length is greater than 30 bytes)

the code u above mentioned is printing the output as

abscder (it is removing extension too )

thanks in advance
saikumar

for file in *
do
  COUNT=$(wc -l < $file)
  DATE=$(date +%Y/%m/%d)
  SUFFIX=${file##*.}
  NAME=${file%.*}
  file=${NAME:0:30}.$SUFFIX
  #echo $file `date +%Y/%m/%d` $count
   printf "%-30s %10s %5d\n " $file $DATE $COUNT
done