Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

diaplay number of lines as a header record 1

Status
Not open for further replies.

navink123

Programmer
Sep 2, 2003
21
US
Hi Guys,

I am extracting data from a table and putting it into a number of text files. While doing this I am displaying the number of records in the file, on the top of the same file as a header record. I am using a modified version of the following script posted by Dickiebird to achieve this.

But the number displayed needs to be a 10 digit number. In case the digits are less than 10, the number needs to be prepended with remaining zeros to make it 10.

Can anybody please help me with this.

Thanks,
Navin
 
Forgot to paste the script

Thanks,
Navin

The existing script is as follows:

for file in *
do
lines=`wc -l $file`
( echo "Lines in file = $lines" ; cat $file ) > tmp$$
mv tmp$$ $file
done
 
Instead of using echo use:

[tt]printf "Lines in file = %010d\n" $lines[/tt]

Annihilannic.
 
This might sound dumb, but try the following,

do a math manipulation:
x 3 33 333
y=10000000000+x
y 10000000003 10000000033 10000000333

use cut to get ride of the leading 1
 
lines=`printf &quot;%010d&quot; &quot;$(wc -l < ${file} )&quot;`

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
If you're using the Korn shell, you can use &quot;[tt]typeset[/tt] to format a variable.
Code:
#!/bin/ksh

typeset -RZ10 lines

for file in test.dat
do
  lines=$(wc -l < $file)
  ( echo &quot;Lines in file = $lines&quot; ; cat $file ) > tmp$$
  mv tmp$$ $file
done
The &quot;[tt]-RZ10[/tt]&quot; sets [tt]lines[/tt] to be Right justified, Zero filled, and 10 characters wide.

Hope this helps.



 
SamBones win! ;)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top