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!

how to change a filename in the loop

Status
Not open for further replies.

navink123

Programmer
Sep 2, 2003
21
US
Hi Guys,
I am using a script to insert a record count on the top of a text file.
But now the source file has changed and the new files are in a compressed format. So I will have to uncompress each file
attach the record count as header and compress it back.

I tried to do something like this. But this script will fail when it reaches line # 7 as variable 'file' will already be assigned a name with extension .Z in line # 6.

How do i overcome this.

Thanks,
N.

1. for file in x[0-9].dat.Z
2. do
3. uncompress file
4. lines=`printf &quot;$(wc -l < ${file} )&quot;`
5. ( echo &quot;$lines&quot; ; cat $file )>tmp$$
6. mv tmp$$ $file
7. compress $file
8. done
 
Something like this ?
Code:
for fileZ in x[0-9].dat.Z
do
  uncompress $fileZ
  file=`basename $fileZ`
  typeset -i lines=$(wc -l < $file)
  (echo &quot;$lines&quot;; cat $file)>tmp$$
  mv tmp$$ $file
  compress $file
done

Hope This Help
PH.
 
Sorry for the typo on the basename line.
Something like this ?
Code:
for fileZ in x[0-9].dat.Z
do
  uncompress $fileZ
  file=`basename $fileZ .Z`
  typeset -i lines=$(wc -l < $file)
  (echo &quot;$lines&quot;; cat $file)>tmp$$
  mv tmp$$ $file
  compress $file
done

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top