[SOLVED] How to pad zeros to a numbrer in the middle of filename
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to pad zeros to a numbrer in the middle of filename
Hi
I have about 30 files with title numbers like this. Here's an example:
title 1 blah blah blah
title 2 blah blah blah
title 3 blah blah blah
.
.
.
title 30 blah blah blah
After title 9 the alphabetic order is broken. I need to pad title 1 to 9 with a zero to keep the files in alphabetic order. If the numbers had some kind of character next to it, like -1, I could easily replace it using sed. But, these have spaces between the numbers. I want the files to look like this
title 01 blah blah blah
title 02 blah blah blah
title 03 blah blah blah
If you only care about order, you can list the files in the "correct" order with ls -v
If you want to rename the files -- if you only want two digits in total, you can just search for a single digit and put a zero in front of it:
Code:
sed -r 's/([^0-9])([0-9])([^0-9])/\10\2\3/'
for three digits, put two zeroes before a single digits, single zero before pairs of digits.
As the number of digits grows, you'll want something more clever than a simple sed to handle it.
If you only care about order, you can list the files in the "correct" order with ls -v
If you want to rename the files -- if you only want two digits in total, you can just search for a single digit and put a zero in front of it:
Code:
sed -r 's/([^0-9])([0-9])([^0-9])/\10\2\3/'
for three digits, put two zeroes before a single digits, single zero before pairs of digits.
As the number of digits grows, you'll want something more clever than a simple sed to handle it.
It Works! I don't understand the code, but it got the job done.
Can you, if possible, explain how the code works? Also, what does \10\2\3/ mean?
It's quite simple, actually. The regexp matches a non-digit followed by a digit followed by a non digit. The parentheses are there so that I can backreference the individual parts with \1, \2, and \3 in the other parts, inserting a '0' between the first non-digit and the digit.
Code:
non-digit
| digit
| | non-digit
| | |
v v v
([^0-9])([0-9])([^0-9]) <----- pattern
<------><-----><------>
\1 \2 \3
|____ | ______|
v v v
\10\2\3 <----- replacement
title 1 blah blah blah
title 2 blah blah blah
title 3 blah blah blah
title 10 blah blah blah
title 11 blah blah blah
title 12 blah blah blah
title 101 blah blah blah
title 102 blah blah blah
title 103 blah blah blah
... this awk ...
Code:
echo " Use this if you want the number of digits"
echo " in column 2 to be 5."
awk '{printf("%s %05d %s %s %s\n"), $1, $2, $3, $4, $5}' $InFile >$OutFile
... produced this OutFile ...
Code:
title 00001 blah blah blah
title 00002 blah blah blah
title 00003 blah blah blah
title 00010 blah blah blah
title 00011 blah blah blah
title 00012 blah blah blah
title 00101 blah blah blah
title 00102 blah blah blah
title 00103 blah blah blah
A refinement: this code ...
Code:
echo " Automatic scaling: Use this if you want the number of digits"
echo " in column 2 to be the same as the largest value."
LoLV=$(cut -d" " -f2 <$InFile |wc -L) # LoLV = Length of Largest Value
awk -v LoLV=$LoLV '{printf("%s %0"LoLV"d %s %s %s\n"), $1, $2, $3, $4, $5}' $InFile >$OutFile
... produced this OutFile ...
Code:
title 001 blah blah blah
title 002 blah blah blah
title 003 blah blah blah
title 010 blah blah blah
title 011 blah blah blah
title 012 blah blah blah
title 101 blah blah blah
title 102 blah blah blah
title 103 blah blah blah
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.