Reindex or re-increment last field of txt file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reindex or re-increment last field of txt file.
# 1  
Old 11-07-2007
Reindex or re-increment last field of txt file.

I am using a database text file with a field that increments +1 with each new entry, occasionally if a entry is deleted the unique sequence is disrupted.

I am looking for a small script/function in sh and/or perl that would re index this.

Example of db file:

Name | Address | misc |number
Me|22 ave|another|1
you|33 rd|something|2
else|7|help|3
neighbor|22 long road||4

If a entry/record is deleted I might end up with this:

Name | Address | misc |number
Me|22 ave|another|1
you|33 rd|something|2
neighbor|22 long road||4

Which I would like to re-index to turn out like this:

Name | Address | misc |number
Me|22 ave|another|1
you|33 rd|something|2
neighbor|22 long road||3

Any help would rock.

Thankyou

Edit: To being with:
awk -F "|" '{print NR-1" "$NF}' filename.txt

Gives two parallel lines for comparison...

Almost there:
awk -F "|" '{$NF=NR-1; print NR-1 " " $NF}' filename.txt

Fixes it, now to figure out the substitue and put it in a nice perl function, which will be hard.

Last edited by silenthands; 11-07-2007 at 08:16 PM.. Reason: Add info.
# 2  
Old 11-08-2007
something like this??

Code:
awk -F "|" -f file.awk filename

where, file.awk contains
Code:
NR!=1 {
for (i=1;i<=NF;i++)
{
 if(NF==i)
 {
  if( $NF != NR-1) printf("%d\n", NR-1); else printf("%s\n", $NF );
 }
 else
 {
  printf("%s|", $i);
 }
}
}

NR==1 { print $0 }

# 3  
Old 11-08-2007
Perfect thank you Smilie

Even took care of the first line which was just headers and no numbers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

2. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

3. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

4. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

5. Shell Programming and Scripting

how to add extra a field in a flat txt file ?

Hi all, I did not use UNIX for a long time, now i need to make a flat file with extra field, can you help me with the code ? 1. I create a last line of each log from each system and make it in a flat text file (seperate by a pipe |) mv current.log old tail -1 sanfrancisco.log > current.log... (5 Replies)
Discussion started by: britney
5 Replies

6. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

7. Shell Programming and Scripting

command to list .txt and .TXT file

Hi expersts, in my directory i have *.txt and *.TXT and *.TXT.log, *.txt.log I want list only .txt and .TXT files in one command... how to ?? //purple (1 Reply)
Discussion started by: thepurple
1 Replies

8. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

9. Shell Programming and Scripting

validate each field in txt

Hello, I have a file with a lot of record like below: 00001,CUSTR,CUSTOMER ADDRESS,02310,N,0:00,0,0,0,0,0,0,0,0,0,0,0,0:00,0,0,0,0,0,CSH,ACC Can I validate each record in the file and output the incorrect result? field 1 - customer number, should be "5 digit. field 2 - should be 5... (9 Replies)
Discussion started by: happyv
9 Replies

10. Shell Programming and Scripting

add increment field when first field changes

Hi I have a file that looks like the following: Name1 aaa bbbb Name1 ffd hhghg Name1 dffg ghhhh Name2 rtrt dfff Name2 rrtfr tgtt Name2 dsddf gfggf Name2 ffffg gfgf NAme3 fdff ghhgh Is it possible to format it so that a number... (2 Replies)
Discussion started by: azekry
2 Replies
Login or Register to Ask a Question