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!

prepending number of records on top of each file

Status
Not open for further replies.

navink123

Programmer
Sep 2, 2003
21
US
I am extracting data from a table and putting it into a number of text files. While doing this, I need to find out how may records are extracted into each file and display it on the top of the same file as a header record.

Anybody has idea how to do this,
Thanks,
Navin
 
Assuming you can't do this whilst generating the file, then you could do this after the file is completed

Code:
#!/bin/sh
#

# count the lines in the file (passed as $1)
# then prepend that information to the file
prepend ( ) {
  lines=`wc -l < $1`
  ( echo &quot;Lines in file = $lines&quot; ; cat $1 ) > tmp$$
  mv tmp$$ $1
}

# example call
prepend results.txt

--
 
Thanks Salem,
This is exactly what I wanted. But the script prepends the # to only one file in the directory. How can I display the # in all files in the directory.

Thanks,
Navin
 
Use a loop to prepend to all files in the directory eg

#!/bin/sh
cd yourdir
for file in *
do
lines=`wc -l $file`
( echo &quot;Lines in file = $lines&quot; ; cat $file ) > tmp$$
mv tmp$$ $file
done



Dickie Bird (:)-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top