LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2015, 02:28 PM   #1
davidvNY
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Rep: Reputation: 0
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

I appreciate any help in this. Thanks
 
Old 05-11-2015, 02:55 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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.
 
Old 05-11-2015, 03:46 PM   #3
davidvNY
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by millgates View Post
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?
 
Old 05-11-2015, 04:10 PM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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
 
1 members found this post helpful.
Old 05-11-2015, 04:13 PM   #5
davidvNY
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: 0
Much appreciated.

+1 rep to you

Last edited by davidvNY; 05-11-2015 at 04:21 PM.
 
Old 05-11-2015, 10:10 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
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
Daniel B. Martin
 
1 members found this post helpful.
Old 05-12-2015, 01:04 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Code:
man rename
 
Old 05-12-2015, 01:40 PM   #8
davidvNY
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: 0
I've already used the code that millgates provided to pad the single digits of my files.

@ danielbmartin

I appreciate the awk code as well. In linux, it's always good tp know additional ways of doing something.

Thanks danielbmartin & syg00 for your replies.
 
Old 05-12-2015, 01:49 PM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this is what i do:
Code:
==> bin/xvidenc-ac3-mkv.ksh <==
#!/bin/bash
################### START OF COMMANDS ###################

dvd=`lsdvd $1 | head -n 1 | awk '{print tolower($3)}' | sed s/_/-/g`
#mkdir /home/xbmc/win/stuff/clips/$dvd
titles=`lsdvd $1 | grep ^Title: | cut -b 8-9 | tail -n 1`
i=1
while [ $i -le 1 ]
do

/usr/bin/mencoder "dvd://$i" -dvd-device $1 -o /dev/null -vc mpeg12 -vf pp=al:c,softskip,unsharp=l5x5:.25:c5x5:.25,harddup -aid 128 -channels 6 -oac lavc -lavcopts acodec=ac3:abitrate=192 -a52drc 1 -ovc xvid -xvidencopts pass=1:turbo:me_quality=6:vhq=4:threads=2:max_bframes=2:bvhq=1:nopacked:quant_type=h263:noqpel:nogmc:trellis:nointerlacing:chroma_me:chroma_opt:hq_ac:nolumi_mask:rc_reaction_delay_factor=0:rc_averaging_period=100:closed_gop:autoaspect -passlogfile "$dvd-$i.log"

/usr/bin/mencoder "dvd://$i" -dvd-device $1 -o "$dvd-`zero-pad-2.x $i`".avi -vc mpeg12 -vf pp=al:c,softskip,unsharp=l5x5:.25:c5x5:.25,harddup -aid 128 -channels 6 -oac lavc -lavcopts acodec=ac3:abitrate=192 -a52drc 1 -ovc xvid -xvidencopts pass=2:bitrate=2000:me_quality=6:vhq=4:threads=2:max_bframes=2:bvhq=1:nopacked:quant_type=h263:noqpel:nogmc:trellis:nointerlacing:chroma_me:chroma_opt:hq_ac:nolumi_mask:rc_reaction_delay_factor=0:rc_averaging_period=100:closed_gop:autoaspect -passlogfile "$dvd-$i.log"




sleep 2

/usr/bin/mkvmerge   --title "$dvd-$title-ac3" --track-name 0:"$dvd-$title-ac3" --language 1:en --track-name 1:"AC3 5.1" "$dvd-`zero-pad-2.x $i`.avi"      -o "$dvd-`zero-pad-2.x $i`.mkv"
sleep 2

i=`expr $i + 1`
done
#################### END OF COMMANDS ####################


==> ./stuff/shh-str-search/zero-pad-2.c <==
#include "stdio.h"
#include "string.h"


char *strrev(char *s){
    char *p=s;
    char *q =s;
    char swap;
    if (*s)
    {
        q=strchr(q,'\0');
        while (--q > p)
        {
            swap = *q;
            *q = *p;
            *p = swap;
            ++p;
        }
    }
    return s;
}


main(int argc, char *argv[])
{
        char col1[255], col2[255];
        int c;

	 while(strlen(argv[1]) < 2)
	 {
          strcpy(argv[1], strrev(argv[1]));
	  strcat(argv[1], "0");
	  strcpy(argv[1], strrev(argv[1]));
	 }
	 printf("s\n", argv[1]);
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell question: pad end of each line with spaces to = 80 chars ?? di11rod Programming 19 04-21-2011 07:03 PM
pad IP addresses with zeros fur Programming 7 06-27-2009 11:02 AM
How do I scp a file with spaces in the filename? glenn69 Linux - General 14 11-01-2007 07:18 PM
Getting rm to like spaces in filename kyosuke Linux - General 2 02-28-2006 04:55 PM
spaces in filename in console...? AlThor880 Linux - Software 4 01-09-2003 02:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration