The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Increment userid in file dejavu88 Shell Programming and Scripting 2 05-26-2008 06:11 PM
add increment field when first field changes azekry Shell Programming and Scripting 2 11-14-2005 04:21 PM
increment a Variable cengiz Shell Programming and Scripting 4 07-13-2005 06:31 AM
Append a field to the end of each line of a file based on searching another file. ultimate Shell Programming and Scripting 2 03-29-2005 11:21 AM
Increment counter in ksh steiner Shell Programming and Scripting 5 12-02-2003 08:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-07-2007
silenthands silenthands is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 2
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 (permalink)  
Old 11-08-2007
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
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 (permalink)  
Old 11-08-2007
silenthands silenthands is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 2
Perfect thank you

Even took care of the first line which was just headers and no numbers.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0