Need a REGEX to increment the file number of a pdf file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a REGEX to increment the file number of a pdf file
# 1  
Old 03-28-2013
Need a REGEX to increment the file number of a pdf file

Hello,

I have a few thousand .pdf files in various folders each have a naming scheme like this:

006_-_Titled_Document_#34_-_September-25-2011-side-1.pdf

In each folder, the number system starts at 001 (as you see on the far left of the file name), and then ends at 999 (maximum .pdf files).

Somewhere in the collection of files say .pdf # 286, I have 286 twice (duplicate). Which screws up my numbering system.

I need a REGEX that I can enter into the shell when I'm in the .pdf directory, and start from say the duplicate # 286 and increment that 2nd duplicate # 286 & all the numbers after that by +1. So that they are all renamed appropriately.

This way I don't have to go in there and rename each .pdf file manually one by one.

Unfortunately, I am not very good with REGEX and haven't had much need for it in the past until now.

Would anyone know the best way to automate this .pdf renumbering task? I would appreciate any constructive thoughts on how to accomplish this.

Thank you.
# 2  
Old 03-28-2013
Well, not regex, just programming in script. I assume the files are in two subtrees or have some name change, and there is a way you want them ordered, so we know who wins on collision? You could name them with temporary fractions with a decimal point and then list them, number them and rename them if they are not name right, starting from the top pulling them up, so there are no collisions.

Last edited by DGPickett; 03-28-2013 at 07:11 PM..
# 3  
Old 03-28-2013
The following script seems to do what you want, but no regular expressions are involved:
Code:
#!/bin/ksh
low=286
low_found=0
ls [0-9][0-9][0-9]_*.pdf|sort -r|while [ $low_found -eq 0 ] && IFS="" read -r fn
do      n=${fn%%_*}
        if [ $n -eq $low ]
        then    low_found=1
        fi
        if [ $n -lt $low ]
        then    break
        fi
        echo mv "$fn" $((n + 1))"${fn#???}"
done

I use (and tested this using) the Korn shell but this will work with any shell that recognizes the parameter expansions required by the POSIX standards. As written it will just list the mv commands that need to be run to do what you requested. If it does what you want remove the echo that appears before the mv command and it rerun it to actually rename the files.
# 4  
Old 03-28-2013
Quote:
Originally Posted by Don Cragun
Code:
ls [0-9][0-9][0-9]_*.pdf|sort -r

Wouldn't

Code:
ls -r [0-9][0-9][0-9]_*.pdf

do the same? "ls" per default sorts alphabetically on file names, "-r" reverses the ordering, no?

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 03-28-2013
Quote:
Originally Posted by bakunin
Wouldn't

Code:
ls -r [0-9][0-9][0-9]_*.pdf

do the same? "ls" per default sorts alphabetically on file names, "-r" reverses the ordering, no?

I hope this helps.

bakunin
Yes. Obviously. I have no idea why I didn't think of this when I was writing this script. Smilie

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a text file and a pdf file from Linux command results.

Hello. The task : Using multiple commands like : gdisk -l $SOME_DISK >> $SOME_FILEI generate some text file. For readiness I must insert page break. When the program is finished I want to convert the final text file to a pdf file. When finished, I got two files : One text file and One pdf... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Convert excel file to PDF file using shell script

Hi All, Is it possible to convert the excel file to PDF file(Without loosing any format) using unix shell scripting ??? If yes Kindly help me on the code Thanks in advance!!! (5 Replies)
Discussion started by: Balasankar
5 Replies

3. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

4. Shell Programming and Scripting

Increment value in text file

Hi Guys, I am new to shell programing, I have a csv file which has 50k records and I have got the requirement to increment the value in second column after each 5000 records. for example below A,B,C,D //Header 1,1,London,UK 1,1,Manchester,UK 1,1,Glasgow,UK . . . 1,1,Newyork,USA... (7 Replies)
Discussion started by: rizzu1555
7 Replies

5. Shell Programming and Scripting

Need to increment number in data file

Hello, I have an Excel spreadsheet with the following data: Refntns3_1 char 30 Ref H77 nt codon 1 Reference H77 Nucleotide Codon 1 -- Codns3_1 char 30 Obs Nucleotides codon 1 Observed Nucleotides Codon 1 ... (2 Replies)
Discussion started by: deneuve01
2 Replies

6. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Solaris

Convert csv file into pdf file from putty

Hi, My requirement is that i have to convenrt a csv file inyo a pdf file . So is there any command which will do that ??? thanks Sambuddha (2 Replies)
Discussion started by: Sambuddha
2 Replies

8. UNIX for Dummies Questions & Answers

Text file + image/form/overlay file to PDF

Hi, We have an app specific legacy environment running SCO Openserver 5.0.7. I need to be able to (1) scan a pre-existing “form” consisting of logo/boxes/lines/static text as an image , (2) lay a print file from the app "on top of the image" and (3) output the "merge" as a PDF file. Scanning... (1 Reply)
Discussion started by: 65bit
1 Replies

9. Shell Programming and Scripting

how to get number of pages in a PDF file

Hello, Can anyone please help me providing script to get the number of pages in a PDF file? TIA Prvn (8 Replies)
Discussion started by: prvnrk
8 Replies

10. UNIX for Advanced & Expert Users

Converting .ps file to .pdf file from where it is broken

In my folder Testing I have 5 files. 1.ps 2.ps 3.ps 4.ps 5.ps Now When I am converting this .ps file to .pdf file then it is broken at the 2.ps file. And I created one status.txt file where this broken file 2.ps is stored. Now How shell I start to convert .ps file to .pdf file from... (5 Replies)
Discussion started by: sunitachoudhury
5 Replies
Login or Register to Ask a Question