Advanced File Rename help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Advanced File Rename help
# 1  
Old 08-01-2008
Advanced File Rename help

So, I am so new that I can't even call myself a unix user. I have an aptitude for programing, but I don't know the language. Anyway, here is my problem:

I have to rename a batch of files that look like:
2001_0001.asc
2001_0002.asc
.
.
.
2006_0548.asc

The names are a date code, but they need to be altered such that 2001_0001.asc becomes 2000_183.asc, 2001_0184 becomes 2001_001, and 2006_0548.asc becomes 2006_365.asc.

so, I think it should start like:

for i in `ls*`
do

then things get a bit hazy
I will now convert to semi-programing speak

split aaaa_bbbb.asc into aaaa and bbbb
interpret strings aaaa and bbbb as numbers x and y respectively
if y - 183 < 1,
then x' = x - 1 and y' = y - 183 + 365,
else x' = x and y' = y - 183.
convert numbers x and y to strings aaaa' and bbb' (note the three digits
in bbb such that y' = 1 will convert to bbb' = 001)
rename file i to aaaa'_bbb'.asc

any help would be appreciated
thanks,
-TT
# 2  
Old 08-01-2008
Code:
for i in *.asc
 
 do 
   
   a=${i%_*}; b=${i#*_}; c=${b%.*}
 
    if [ $(expr $c - 183) -lt 1 ]
      then
         x=$(expr $a - 1); y=$(printf "%03s\n" $(expr $c - 183 + 365))
         mv $i $x"_"$y.asc
      else 
         x=$a; y=$(printf "%03s" $(expr $c - 183))
         mv $i $x"_"$y.asc
    fi
 
done

Try it first with echo, then do a rename with mv.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

4. Shell Programming and Scripting

Advanced: Sort, count data in column, append file name

Hi. I am not sure the title gives an optimal description of what I want to do. Also, I tried to post this in the "UNIX for Dummies Questions & Answers", but it seems no-one was able to help out. I have several text files that contain data in many columns. All the files are organized the same... (14 Replies)
Discussion started by: JamesT
14 Replies

5. SCO

Advanced File and Print Server (AFPS)

I'm looking for a 25-user AFPS license - this was a product available on SCO UnixWare 7.1.3 (was superseded by Samba in 7.1.4) Anyone have one sitting at the back of a cupboard/drawer/garage... I have a buyer lined up! (1 Reply)
Discussion started by: billbateson
1 Replies

6. UNIX for Advanced & Expert Users

Advanced file transfer

Hey all. I need to transfer files from one system to about 5 other systems, and i have to do it manually, and we do get a lot of requests for the same. Presently this is done using a shell script, but it has to be run on each of the 5 systems. Also, a backup of the file needs to be preserved... (6 Replies)
Discussion started by: optimus_1
6 Replies

7. Shell Programming and Scripting

Advanced Search & Delete Text File

I have a file in which email messages are stored in. Every email is separated by by a ^Z character (Control-Z). I need to extract all emails after the 65,00th one to another file and delete them from the original file. Any suggests on accomplishing this? (2 Replies)
Discussion started by: maxcell
2 Replies

8. UNIX for Dummies Questions & Answers

Rename file based on first 3 characters of data in file

I'm looking to determine if I can use a grep command to read file and rename the file based on the first 3 characters of the data in the file. An example is: Read FileA If the first 3 positions of the data in the file are "ITP", then rename the file as FileA_ITP, else if the first 3... (3 Replies)
Discussion started by: jchappel
3 Replies

9. UNIX for Advanced & Expert Users

advanced file renaming problem

I know this is probably a question for the newbie forum, where it is also posted, but I thought maybe some of you pros might like to help me out anyway. Here is my problem: I have to rename a batch of files that look like: 2001_0001.asc 2001_0002.asc . 2001_0548.asc 2002_0184.asc . .... (0 Replies)
Discussion started by: sea krait
0 Replies

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question