Batch to add FORMATED line numbers

August 23, 2010 at 08:12:54
Specs: Windows XP
There was a discussion about "Batch to add line numbers".
All the answers weren't really useful if you want to be able to parse and sort back, because the line numbers generated by FIND FINDSTR and GAWK are UNFORMATED.

The resulting files which receive line numbers are like this. Example with FINSDTR and a file with 101 lines, and the 1st line is EMPTY :
---------------- Input File :

Text-Line
Text-Line
Text-Line
Text-Line
... etc 100 times ...
----------------
---------------- Output File :
1:
2:Text-Line
3:Text-Line
4:Text-Line
5:Text-Line
6:Text-Line
7:Text-Line
8:Text-Line
9:Text-Line
10:Text-Line
11:Text-Line
... etc ...
98:Text-Line
99:Text-Line
100:Text-Line
101:Text-Line
----------------
---------------- SORTED Output File :
1:
10:Text-Line
100:Text-Line
101:Text-Line
11:Text-Line
12:Text-Line
13:Text-Line
14:Text-Line
15:Text-Line
16:Text-Line
17:Text-Line
18:Text-Line
19:Text-Line
2:Text-Line
20:Text-Line
21:Text-Line
... etc ...
88:Text-Line
89:Text-Line
9:Text-Line
90:Text-Line
91:Text-Line
92:Text-Line
93:Text-Line
94:Text-Line
95:Text-Line
96:Text-Line
97:Text-Line
98:Text-Line
99:Text-Line
----------------

On top of that, if the text file is UTF-8, you get the "UTF-8 header code" inside the 1st line of you resulting file (no matter which method used).

SO, ANY IDEA HOW TO "ADD FORMATED LINE NUMBERS" ???


See More: Batch to add FORMATED line numbers

Report •


#1
August 23, 2010 at 09:19:50
I dunno UTF8 but if you provide a file I'll look at it.

The solution to make numbered lines "sortable" is to pad the nums.

0001
0002
...
9999


=====================================
Helping others achieve escape felicity

M2


Report •

#2
August 23, 2010 at 16:31:02
UTF-8 is an extended character encoding option, which you can find in web-pages for instance, in order to be able to use "uncommon" characters (Chinese characters, Eastern European characters, etc ...).
If you use Notepad, you can save a text file using the UTF-8 encoding option.

What do you add in your DOS Batch command line to pad the numbers using GAWK (for instance) ???
_____________________________________________________________

@ECHO OFF

if exist lined.txt erase lined.txt

rem ********************************************************************************
rem **** What should be added to the next command line in order to ****
rem **** pad the line numbers ??? ****
rem ********************************************************************************
gawk "{print NR\":\"$0}" test.txt>lined.txt

sort lined.txt>lines.txt

start notepad lines.txt

if exist lined.txt erase lined.txt

CLS
EXIT
_____________________________________________________________



Report •

#3
August 23, 2010 at 17:35:23
so you want to pad the numbers according to how many lines in the file right?

gawk "FNR==NR{n=NR;len=length(n);next} {printf \"%0\"len\"s:%s\n\",FNR,$0}" file file

by the way, i see you using windows sort. I recommend using GNU's sort which is in coreutils for windows . It has added functionality such as sorting by columns and characters etc

GNU win32 packages | Gawk


Report •

Related Solutions

#4
August 23, 2010 at 19:44:28
GOOD LAWD !

It doesn't really work.
I did some C, here's the source (plain brutal programming) :
Project : AddLiNum
Executable : AddLiNum.exe
Command Line :
AddLiNum /n "InputFile" "OutputFile"
/n : Number of digits of the "line numbers"
n = value between 1 and 9

If you want to put a separator between the "line numbers" and the "lines", just use this command line before launching the program :
gawk "{print \":\"$0}" InputFile>OutputFile

If someone wants to put the "AddLiNum.exe" online, it's OK with me, I won't claim for copyright on such "cheap code", Ah Ah Ah !

C Source :
__________________________________________________

#include <stdio.h>
#include <stdlib.h>
#include <string.h> //---- Traitements sur chaînes de caractères ----

//----FONCTION----Inclue un numéro de ligne au début de chaque ligne d'un
//----------------fichier.
//----------------Le RETURN donne une valeur numérique exploitable en
//----------------tant que ERRORLEVEL (par un fichier .bat par exemple).
//----------------Les valeurs du RETURN sont :
//---------------- 0 : la numérotation des lignes du fichier a réussi,

int main ( int argc , char * argv [] )
{

//----------------VARIABLES (déclaration)

FILE * ptr_fichier_entree ;
char nom_fichier_entree[1024] ;
char ligne_fichier_entree[2048] ;
char mode_fichier_entree[8] = "r" ;

char switch_entree[256] ;
int switch_val ;

FILE * ptr_fichier_sortie ;
char nom_fichier_sortie[1024] ;
char ligne_fichier_sortie[2048] ;
char mode_fichier_sortie[8] = "w+" ;

long num_ligne_num = 0 ;
char num_ligne_char[9] ;
char num_ligne_buff[9] ;
int num_ligne_buff_length ;

char chaine_caracteres_tampon[2048] ;

long erreur = 1 ;

//----------------BEGIN (programme)

system("@ECHO OFF");

//----------------Test of input values

if ( argc != 4 ) goto err ;

// RÉCUPÉRATION DE LA VALEUR DU SWITCH ENTRÉ.
strcpy ( switch_entree , &argv[1][0] ) ;

if ( strlen ( switch_entree ) != 2 ) goto err ;

switch_val = 0 ;
if ( strcmp ( &switch_entree[0] , "/1" ) == 0 ) switch_val = 1 ;
if ( strcmp ( &switch_entree[0] , "/2" ) == 0 ) switch_val = 2 ;
if ( strcmp ( &switch_entree[0] , "/3" ) == 0 ) switch_val = 3 ;
if ( strcmp ( &switch_entree[0] , "/4" ) == 0 ) switch_val = 4 ;
if ( strcmp ( &switch_entree[0] , "/5" ) == 0 ) switch_val = 5 ;
if ( strcmp ( &switch_entree[0] , "/6" ) == 0 ) switch_val = 6 ;
if ( strcmp ( &switch_entree[0] , "/7" ) == 0 ) switch_val = 7 ;
if ( strcmp ( &switch_entree[0] , "/8" ) == 0 ) switch_val = 8 ;
if ( strcmp ( &switch_entree[0] , "/9" ) == 0 ) switch_val = 9 ;
if ( switch_val == 0 ) goto err ;

// RÉCUPÉRATION DU NOM DE FICHIER EN ENTRÉE PASSÉ AU PROGRAMME.
strcpy ( nom_fichier_entree , &argv[2][0] ) ;

// RÉCUPÉRATION DU NOM DE FICHIER EN SORTIE PASSÉ AU PROGRAMME.
strcpy ( nom_fichier_sortie , &argv[3][0] ) ;

if ( strcmp ( &nom_fichier_entree[0] , &nom_fichier_sortie[0] ) == 0 )
{
printf ("Command Line Error :\n");
printf (" InputFile is identical to OutputFile\n");
return 1 ;
}

// TENTATIVE D'OUVERTURE DU FICHIER EN ENTRÉE EN MODE LECTURE SIMPLE.
ptr_fichier_entree = fopen ( &nom_fichier_entree[0] , &mode_fichier_entree[0] ) ;
if ( ptr_fichier_entree == NULL ) // le fichier n'existe pas
{ // ou les droits sont insuffisants !
printf ("The InputFile doesn't exist or is already in use.\n");
return 2 ;
}

// TENTATIVE D'OUVERTURE DU FICHIER EN SORTIE EN MODE ÉCRASEMENT-CRÉATION.
ptr_fichier_sortie = fopen ( &nom_fichier_sortie[0] , &mode_fichier_sortie[0] ) ;
if ( ptr_fichier_sortie == NULL ) // le fichier sorti est tenu par une autre
{ // appli ou les droits sont insuffisants !
printf ("The OutputFile can't be created or is already in use.\n");
return 3 ;
}

//----------------END OF Test of input values

//----------------DÉBUT DU TRAITEMENT

strcpy ( ligne_fichier_entree , "" ) ;
strcpy ( ligne_fichier_sortie , "" ) ;
fgets ( ligne_fichier_entree , 2048 , ptr_fichier_entree ) ;

while ( ! feof ( ptr_fichier_entree ) )
{
num_ligne_num = num_ligne_num + 1 ;
itoa ( num_ligne_num, num_ligne_buff, 10 ) ;
strcpy ( num_ligne_char , "000000000" ) ;
strcat ( num_ligne_char , num_ligne_buff ) ;
strcpy ( ligne_fichier_sortie , &num_ligne_char[ strlen (num_ligne_buff) + 9 - switch_val ] ) ;
strcat ( ligne_fichier_sortie , ligne_fichier_entree ) ;
fputs ( ligne_fichier_sortie , ptr_fichier_sortie ) ;
strcpy ( ligne_fichier_entree , "" ) ;
strcpy ( ligne_fichier_sortie , "" ) ;
fgets ( ligne_fichier_entree , 2048 , ptr_fichier_entree ) ;
}
num_ligne_num = num_ligne_num + 1 ;
itoa ( num_ligne_num, num_ligne_buff, 10) ;
strcpy ( num_ligne_char , "000000000" ) ;
strcat ( num_ligne_char , num_ligne_buff ) ;
strcpy ( ligne_fichier_sortie , &num_ligne_char[ strlen (num_ligne_buff) + 9 - switch_val ] ) ;
strcat ( ligne_fichier_sortie , ligne_fichier_entree ) ;
fputs ( ligne_fichier_sortie , ptr_fichier_sortie ) ;

//----------------FIN DU TRAITEMENT

erreur = 0 ;
err:
if ( erreur != 0 )
{
printf ("Command Line Error :\n");
printf (" The correct command line should be:\n");
printf (" AddLiNum /n \"InputFile\" \"OutputFile\"\n");
printf (" /n : Number of digits of the \"line numbers\"\n");
printf (" n = value between 1 and 9\n\n");
return 1 ;
}

//----------------Test of input values


fclose ( ptr_fichier_entree ) ;
fclose ( ptr_fichier_sortie ) ;

return 0 ;

//----------------END (programme)
}
__________________________________________________

It's not "elegant", but it works !!!


Report •

Ask Question