Substract a certain number to the names of several files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Substract a certain number to the names of several files
# 1  
Old 01-03-2010
Substract a certain number to the names of several files

We have a list of files (raw and jpeg types) with 2 different extensions (rw2 and jpg).
When there is both the raw and jpeg files, their file numbers must be the same (215 and 215, 218 and 218).
Sometimes there is only the jpeg file (216,217).

bla_215.rw2
bla_215.jpg
bla_216.jpg
bla_217.jpg
bla_218.rw2
bla_218.jpg etc

Is it possible to have a new numerotation starting from 001 AND respecting the rules above?

Maybe substract 214 so that 215 becomes 001, 216 becomes 002, etc
BUT 215.cr2 and 215.jpg must stay linked together by their new numbers: 001.cr2 and 001.jpg?

Thank's.
# 2  
Old 01-03-2010
For a directory listing you could try this:
Code:
ls | while read name
do
  ext=${name##*.}
  first=${name%.*}
  pre=${first%%_*}
  nr=${first##*_}
  newnr=$(printf "%03d" $((nr-214)))
  mv "$name" "${pre}_${newnr}.${ext}"
done

# 3  
Old 01-03-2010
or try awk..
Code:
for file_name in * ; do
to_file=`awk -F"[_.]" '{printf "%s_%03d.%s",$1,$2-214,$3}' $file_name`
mv $file_name $to_file
done

# 4  
Old 01-03-2010
Hi Scrutinizer, thank you for answering.

Seems like the subtraction doesn't work:

philippe@ubuntu-linux-bqf:~/Z_exclu_de_sauvegarde/test$ filenamesubstract.sh
philippe@ubuntu-linux-bqf:~/Z_exclu_de_sauvegarde/test$ ls
POO215_-214.CR2 POO215_-214.JPG POO216_-214.CR2 POO216_-214.JPG
# 5  
Old 01-03-2010
Hi Epictete,

I was using the _ character as part of the selection pattern, because that was in your example. If your actual files do not contain a _ character before the number then it does not work.

You can also use another criterion, e.g. the part of the filename before the number contains no numbers:

e.g:

Code:
ls | while read name
do
  ext=${name##*.}
  first=${name%.*}
  pre=${first%%[0-9]*}
  nr=${first##*[^0-9]}
  newnr=$(printf "%03d" $((nr-214)))
  mv "$name" "${pre}${newnr}.${ext}"
done

# 6  
Old 01-03-2010
Sorry for the wait Scrutinizer but I was trying to guess how the 2 scripts worked.

In fact it works perfectly:

philippe@ubuntu-linux-bqf:~/Z_exclu_de_sauvegarde/test$ filenamesubtract.sh
philippe@ubuntu-linux-bqf:~/Z_exclu_de_sauvegarde/test$ ls
POO001.CR2 POO001.JPG POO002.CR2 POO002.JPG

Would you give some indications about what the script is doing?
# 7  
Old 01-03-2010
OK, here it is with annotation:
Code:
ls | while read name                    
do
  ext=${name##*.}                      # ext becomes everything after the last dot
  first=${name%.*}                     # first becomes everything before the last dot
  pre=${first%%[0-9]*}                 # pre becomes every non-digit from the start (using the variable first)
  nr=${first##*[^0-9]}                 # nr becomes everything from the first digit until the end (using the variable first)
  newnr=$(printf "%03d" $((nr-214)))   # newnr becomes nr minus 214 and preambled with 0's so that it is always 3 digits.
  mv "$name" "${pre}${newnr}.${ext}"
done

This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting a column according to the number of names

Hey, So I'm having issues sorting a data set. The data set contains entries as such; # key: sex, time, athlete, athlete's nationality, date, city, country M, 2:30:57.6, Harry Payne, GBR, 1929-07-05, Stamford Bridge, England M, 2:5:42, Khalid Khannouchi, MAR, 1999-10-24, Chicago, USA M,... (1 Reply)
Discussion started by: DNM_UKN
1 Replies

2. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

3. Shell Programming and Scripting

Compare two files containing package names and version number

I have 2 files each containing a list of same fedora packages but with different version number. I want to compare the 2 files and remove the lines containing a newer or older version number (1 Reply)
Discussion started by: asya18
1 Replies

4. Shell Programming and Scripting

Substract 1 Month from MonthID

Hi, I am writing a small unix command to substract one month from the MonthID. If MonthID = 201301 the below script returns me 201212 if ] then a=`echo $monthid | cut -c1-4` b=`expr $a - 1` c=12 echo "$b$c" else a=`echo $monthid | cut -c5-6` b=`expr $a - 1` c=`echo $monthid | cut... (4 Replies)
Discussion started by: pinnacle
4 Replies

5. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

6. Shell Programming and Scripting

Substract and print

Dear all, I need your help. I have file input like this: input.txt: R1031 50111G1 R1031 50121G1 R1031 50131G1 R1031 50141G1 R1031 50151G1 . . . . Desired output: 10315011 = G, 10315012 =... (2 Replies)
Discussion started by: attila
2 Replies

7. Solaris

Substract time between two columns

I've start and end time in two columns. How can I substract time from column 2 and column 1 and receive output in another file 'd' ? $ cat c 12:55:04 2:03:56 2:03:56 3:20:17 14:00:00 13:05:00 (1 Reply)
Discussion started by: alps0206
1 Replies

8. Shell Programming and Scripting

Find the number of non-duplicate names recursively.

Hi, here comes another newbie question: How to find the number of non-duplicate names recursively? For example, my files are stored in the folders like: If I do find . -depth -name "*.txt" | wc -l This will gives out a result "4". One .txt file named "1.txt" in folder "1", and... (2 Replies)
Discussion started by: jiapei100
2 Replies

9. Shell Programming and Scripting

Finding files with names that have a real number greater then difined.

I am trying to find all files in a directory whose name has a real number larger then the number I am looking for. For example: . |-- delta.1.5.sql |-- delta.2.1.sql |-- delta.2.2.sql |-- delta.2.3.sql |-- delta.2.4.sql `-- delta.2.5.sql I know my database is at 2.2 so I want an... (2 Replies)
Discussion started by: harmonwood
2 Replies

10. UNIX for Dummies Questions & Answers

total number of files which have "aaa" in files whose names are File*_bbb*

I am getting the list of all the files which have "aaa" from files whose name is File*_bbb*. grep -l "aaa" File*_bbb* But I want to count the number of files. That is I want the total number of files which have "aaa" in files File*_bbb* If I run the following for getting number of... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies
Login or Register to Ask a Question