adding order number in the output file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding order number in the output file
# 15  
Old 03-12-2007
Quote:
Originally Posted by jacoden
Hi
Try this using python:'input_file' is the in[put file and o/p will be in out_file
Save the below in a file(,say 'script_file') and Run " python script_file

Code:
s_list=open('input_file',"r").readlines()
f_out=open('out_file',"a+")
for each in s_list:
        init_part=each[:-6]
        f_out.writelines("%s%05d\n" %(init_part,(int(each[5:]) + 1)))
f_out.close()


Thnx,
Dennis
This should be fine evenif the names contains number.Also I believe other awk scripts also should be giving the expected o/p.
# 16  
Old 03-12-2007
Quote:
Originally Posted by anbu23
Whats the problem with this code? I am getting correct result.
Code:
$ cat file
maryu01001
johnm00003
peter02234
catmy66751
$ awk '{
>    n = substr( $0, length( $0 ) - 4, 5 )
>    n=sprintf("%05d", ++n)
>    sub( /[0-9]{5}/, n , $0 )
>    print
> }' file
maryu01002
johnm00004
peter02235
catmy66752


For example, the below record will be error.

ma01u01002
jo02nm00004
p01ter02235
# 17  
Old 03-13-2007
Please try this

Code:
$ cat smp.csh
#!/bin/ksh
for i in `cat smp`
do
num=`echo $i | sed 's/^.*[a-zA-Z]0*\([0-9]*\)$/\1/'`
chr=`echo $i | sed 's/^\(.*[a-zA-Z]0*\)[0-9]*$/\1/'`
(( num1 = num + 1 ))
print $chr$num1
done

Code:
$ cat smp
maryu01001
johnm00003
p0eter02234
catmy66751
dfsd0001100
ma01u01002
jo02nm00004
p01ter02235
$ ./smp.csh
maryu01002
johnm00004
p0eter02235
catmy66752
dfsd0001101
ma01u01003
jo02nm00005
p01ter02236
$

# 18  
Old 03-13-2007
Bug

Give a try on this....[[ If you do not like my python script ]]

awk '{ c=substr($0,0,5); n=substr( $0, length( $0 ) - 4,5); n++; printf "%s%05d\n",c,n ; }' filename

Thnx.Dennis
# 19  
Old 03-13-2007
Quote:
Originally Posted by jacoden
Give a try on this....[[ If you do not like my python script ]]

awk '{ c=substr($0,0,5); n=substr( $0, length( $0 ) - 4,5); n++; printf "%s%05d\n",c,n ; }' filename

Thnx.Dennis
Code:
u142115@linux2alm:~/aps/aps4/product/den> cat temp
m5ryu01001
j0hnm00003
peter02234
catmy66751
u142115@linux2alm:~/aps/aps4/product/den> awk '{ c=substr($0,0,5); n=substr( $0, length( $0 ) - 4,5); n++; printf "%s%05d\n",c,n ; }' temp
m5ryu01002
j0hnm00004
peter02235
catmy66752
u142115@linux2alm:~/aps/aps4/product/den>

# 20  
Old 03-13-2007
Quote:
Originally Posted by happyv
For example, the below record will be error.

ma01u01002
jo02nm00004
p01ter02235
Code:
$ cat file
ma01u01002
jo02nm00004
p01ter02235
$ awk '{
>    n = substr( $0, length( $0 ) - 4, 5 )
>    n=sprintf("%05d", ++n)
>    sub( /[0-9]{5}/, n , $0 )
>    print
> }' file
ma01u01003
jo02nm00005
p01ter02236

Dont you get this results?
# 21  
Old 03-13-2007
Quote:
Originally Posted by anbu23
Code:
$ cat file
ma01u01002
jo02nm00004
p01ter02235
$ awk '{
>    n = substr( $0, length( $0 ) - 4, 5 )
>    n=sprintf("%05d", ++n)
>    sub( /[0-9]{5}/, n , $0 )
>    print
> }' file
ma01u01003
jo02nm00005
p01ter02236

Dont you get this results?

Great....well done!!! It's work
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding user name to file, and then displaying new line number

Hi all - I'm completely stumped by a script I'm working on... The short version is I have a file called 'lookup' and in it are hundreds of names (first and last). I have a script that basically allows the user to enter a name, and what I need to have happen is something like this: Record... (8 Replies)
Discussion started by: sabster
8 Replies

2. Shell Programming and Scripting

Need Help in adding sequence number to a file

Hi All , I have a file which contains data(comma separated) in below format : 500,Sourav ,kolkata ,8745775020,700091 505,ram,delhi ,9875645874,600032 510 ,madhu ,mumbai ,5698756430 ,500042 515 ,ramesh ,blore ,8769045601 ,400092 I want to add unique sequence number at the start of each... (7 Replies)
Discussion started by: STCET22
7 Replies

3. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

4. Shell Programming and Scripting

adding line number to *end* of records in file

Given a file like this: abc def ghi I need to get to somestandardtext abc1 morestandardtext somestandardtext def2 morestandardtext somestandardtext ghi3 morestandardtext Notice that in addition to the standard text there is the line number added in as well. What I conceived is... (4 Replies)
Discussion started by: edstevens
4 Replies

5. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

6. Shell Programming and Scripting

Adding number before file extension

Hi , I have a file which has a line starts with $segment_name and has the below data source data $Segment_Name = 123.ABC.123.01.txt $Segment_Name = 123.ABC.ABC.txt $Segment_Name = 123.ABC.12A3.txtMy target data should be $Segment_Name = 123.ABC.123.01.txt $Segment_Name =... (2 Replies)
Discussion started by: shruthidwh
2 Replies

7. UNIX for Dummies Questions & Answers

Adding Sequence Number to file

Hi All, I need to create a script which checks for a particular file for eg.kumar1.txt. If kumar1.txt is already exist the script should increment the file name as kumar2.txt and so on. Please Advise. Thanks & Regards, Kumar66 (2 Replies)
Discussion started by: kumar66
2 Replies

8. Shell Programming and Scripting

How to split a file with adding sequence number and extension.

I have a file name -HRCFTSIN05PLA1602100430444444 my requirement is to split the file in 10000 count each file and to add sequence number.rch at the end of each file. output should be in this format HRCFTSIN05PLA160210043044444401.rch HRCFTSIN05PLA160210043044444402.rch... (4 Replies)
Discussion started by: abhigrkist
4 Replies

9. Shell Programming and Scripting

Counts a number of unique word contained in the file and print them in alphabetical order

What should be the Shell script that counts a number of unique word contained in a file and print them in alphabetical order line by line? (7 Replies)
Discussion started by: proactiveaditya
7 Replies

10. Shell Programming and Scripting

Adding a sequence number within a file

Can someone please help. I need to add a sequence number to the start of each line of a file. It needs to be 6 characters long. ie 000001 next line starts 000002 etc. (4 Replies)
Discussion started by: Dolph
4 Replies
Login or Register to Ask a Question