Replace and Increment a value in the fixed length file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace and Increment a value in the fixed length file
# 1  
Old 08-06-2014
Question Replace and Increment a value in the fixed length file

Hi Everyone,

I need to increment a value in the fixed length file. The file has almost a million rows. Is there any easy way to accomplish this.

Ex
input file
Code:
ASDSD ADSD 00000 X
AAASD ADSD 00000 X
SDDDD ADSD 00000 X

Ouput
Code:
ASDSD ADSD 00001 X
AAASD ADSD 00002 X
SDDDD ADSD 00003 X

Thanks in advance.

Last edited by Corona688; 08-06-2014 at 03:22 PM..
# 2  
Old 08-06-2014
Please use code tags as required by forum rules!

I don't think you can fit a million into 6 decimal digits.
# 3  
Old 08-06-2014
Sorry about that RudiC. Those are not decimal values in the file and I did not provide no of positions and it should have been 7 digits. The sample value more precisely

Code:
ASDSD ADSD 0000000 X
AAASD ADSD 0000000 X
SDDDD ADSD 0000000 X


output
Code:
ASDSD ADSD 0000001 X
AAASD ADSD 0000002 X
SDDDD ADSD 0000003 X


Last edited by Corona688; 08-06-2014 at 04:20 PM..
# 4  
Old 08-06-2014
Please use code tags, not icode tags.

Try
Code:
awk '{$3=sprintf ("%07.f", NR)}1' file
ASDSD ADSD 0000001 X
AAASD ADSD 0000002 X
SDDDD ADSD 0000003 X

This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-06-2014
Hi Rudic,
I have tried the command but the length of the file is modified everytime I run the command. For ex

Code:
ASDSD ADSD 0000001    X
AAASD ADSD 0000001    X
SDDDD ADSD 0000001    X

Modified to

Code:
ASDSD ADSD 0000001 X
AAASD ADSD 0000002 X
SDDDD ADSD 0000003 X

# 6  
Old 08-07-2014
if column 3 value is constant, use below code

Code:
awk '{newnum=sprintf ("%07.f", NR);sub("0000001",newnum,$0)}1' filename

This User Gave Thanks to pravin27 For This Post:
# 7  
Old 08-07-2014
That is not what you gave us as a sample file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate fixed length txt file

hi, i am using below query to generate the fixed length txt file. this sql is being called from shell script. This is supposed to be a fixed record file with the below definitions. There must be 2 byte filler after the CAT_ID AND each line should have total of 270 bytes. field ... (1 Reply)
Discussion started by: itzkashi
1 Replies

2. UNIX for Dummies Questions & Answers

Length of a fixed width file

I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers. awk '{print length;exit}' <File_name> The above code gives me length 50. wc -L <File_name> The above code gives me length 53. Please clarify on... (2 Replies)
Discussion started by: Amrutha24
2 Replies

3. Shell Programming and Scripting

Replace Date in a fixed length file

Hello All, I working on ksh. I am using fixed length file. My file is like: ======== IXTTIV110827 NANTH AM IKSHIT ABCDEF 0617 IJAY NAND EENIG ZXYWVU 0912 AP OOK OONG PQRSTU100923 NASA DISH TTY ASDFG 0223 GHU UMA LAM QWERT 0111 ATHE SH THEW ======= From 7th to 12 is a date... (4 Replies)
Discussion started by: AnanthaDikshit
4 Replies

4. Shell Programming and Scripting

Fixed length flat file extraction

Hii ,I am new to Unix ,i have a flat file which is (fixed length) sitting in unix,Which is holding the data for a table.I want to extract one column(length7-10) on the basis of another column(length13-15) and want only one single row Example: Below is the sample of flat file. 1111 AAAA 100 ... (4 Replies)
Discussion started by: laxmi1166
4 Replies

5. Shell Programming and Scripting

Search and replace particular characters in fixed length-file

Masters, I have fixed length input file like FHEAD0000000001XXXX20090901 0000009000Y1000XXX2 THEAD000000000220090901 ITM0000109393813 430143504352N22SP 000000000000RN000000010000EA P0000000000000014390020090901 TTAIL0000000003000000 FTAIL00000000040000000002 Note... (4 Replies)
Discussion started by: bittoo
4 Replies

6. Shell Programming and Scripting

search and replace fixed length record file

Hi I need to be search a file of fixed length records and when I hit a particular record that match a search string, substitute a known position field In the example file below FHEAD000000000120090806143011 THEAD0000000002Y0000000012 P00000000000000001234 TTAIL0000000003... (0 Replies)
Discussion started by: nedkelly007
0 Replies

7. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

8. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

9. Shell Programming and Scripting

sed replace with fixed length

$ cat template s.noName $ sed "s/s.no/1/" template > out $ sed "s/s.no/100/" template >>out $ cat out 1Name 100Name 1 Name 100Name (7 Replies)
Discussion started by: McLan
7 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question