Computing.Net > Forums > Unix > Renaming files with awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Renaming files with awk

Reply to Message Icon

Name: meak99
Date: February 21, 2008 at 05:13:53 Pacific
OS: Sun 9
CPU/Ram: ?
Product: Sun
Comment:

Looking for some help in renaming files using a shell script.

Suppose I have a directory which contains several files, some of which are *.dat files. If there are 7 *.dat files, I would like to rename all 7 *.dat files to *.01, *.02, ... *.07

Here is what I have thus far:

ls -l *.dat $*| awk `

NF == 9 && /^-/{ #can someone explain this? this seems to loop through $
++filenum #increments to count number of *.dat files
print filenum, "\t", $9 #prints filenumber and current *.dat filename
newfile= $9 ".0" filenum #produces *.dat.01, but want *.01, *.02, etc
mv $9 $newfile # seems to do nothing, why?
}`

Any help is greatly appreciated!



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: February 21, 2008 at 08:55:43 Pacific
Reply:

I'm having a problem following your awk code. One thing is sure: you can NOT embed other shell commands like mv inside an awk script - not without using the awk system command.

How about this script:

#!/bin/ksh

typeset -2Z i

i=0
ch /path/to/mydir
find . -type f -name "*.dat" -print|while read fname
do
((i=i+1))
echo "$i"
echo "$fname"
mv "$fname" "$fname.$i"
done

The above scrpt might have problems if your directory has subdirectories with files named *.dat or if any of your file names contain a space.


0

Response Number 2
Name: meak99
Date: February 22, 2008 at 04:55:41 Pacific
Reply:

Thank you -

With a little tweaking, I think what you provided will work. I added some code to replace the .dat with .01, etc. rather than append.

My only issue now is that I can't figure out what order it is applying these numbers in. When I sort by filename, the numbers are coming out *.04, *.01, *.03, *.01 - I would have thought it would take my input files in order of filename.

Also what is "ch" - this is not found by my system, and might have something to do with the ordering?

Thanks nails.


0

Response Number 3
Name: nails
Date: February 23, 2008 at 12:17:24 Pacific
Reply:

First, I'm a bozo. That "ch" is suppose to be a "cd" for change directory. I apologize.

Second, I don't know what to tell you about the sort issue without seeing the data and how you are using the sort command.

Oh, and you're welcome.


0

Response Number 4
Name: meak99
Date: February 26, 2008 at 04:01:56 Pacific
Reply:

Well, I am not really using a "sort" command. It is picking the order in which to rename the files - and it doesn't appear to be based on filename, or file size.

Ideally, I'd like it to sort by filename prior to renaming.

Other than that, everything is working great.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


multiple grep URGENT-check data within ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Renaming files with awk

Bash: rename files with 2 spaces www.computing.net/answers/unix/bash-rename-files-with-2-spaces/7140.html

trouble merging two files with awk www.computing.net/answers/unix/trouble-merging-two-files-with-awk/7937.html

Read tow files with AWK www.computing.net/answers/unix/read-tow-files-with-awk/6634.html