![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
something like this??
Code:
awk -F "|" -f file.awk filename 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 }
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|