Sponsored Content
Top Forums Shell Programming and Scripting Reindex or re-increment last field of txt file. Post 302144372 by silenthands on Wednesday 7th of November 2007 05:59:05 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
NDP(8)							    BSD System Manager's Manual 						    NDP(8)

NAME
ndp -- control/diagnose IPv6 neighbor discovery protocol SYNOPSIS
ndp [-nt] hostname ndp [-nt] -a | -c | -p ndp [-nt] -r ndp [-nt] -H | -P | -R ndp [-nt] -A wait ndp [-nt] -d hostname ndp [-nt] -f filename ndp [-nt] -i interface [expressions ...] ndp [-nt] -I [interface | delete] ndp [-nt] -s nodename etheraddr [temp] [proxy] DESCRIPTION
The ndp command manipulates the address mapping table used by the Neighbor Discovery Protocol (NDP). -a Dump the currently existing NDP entries. The following information will be printed: Neighbor IPv6 address of the neighbor. Linklayer Address Linklayer address of the neighbor. It could be ``(incomplete)'' when the address is not available. Netif Network interface associated with the neighbor cache entry. Expire The time until expiry of the entry. The entry could become ``permanent'', in which case it will never expire. S State of the neighbor cache entry, as a single letter: N Nostate W Waitdelete I Incomplete R Reachable S Stale D Delay P Probe ? Unknown state (should never happen). Flags Flags on the neighbor cache entry, in a single letter. They are: Router, proxy neighbor advertisement (``p''). The field could be followed by a decimal number, which means the number of NS probes the node has sent during the current state. -A wait Repeat -a (dump NDP entries) every wait seconds. -c Erase all the NDP entries. -d Delete specified NDP entry. -f Parse the file specified by filename. -H Harmonize consistency between the routing table and the default router list; install the top entry of the list into the kernel rout- ing table. -I Shows the default interface used as the default route when there is no default router. -I interface Specifies the default interface to be used when there is no interface specified even though required. -I delete The current default interface will be deleted from the kernel. -i interface [expressions ...] View ND information for the specified interface. If additional arguments expressions are given, ndp sets or clears the flags or variables for the interface as specified in the expression. Each expression should be separated by white spaces or tab characters. Possible expressions are as follows. Some of the expressions can begin with the special character '-', which means the flag speci- fied in the expression should be cleared. Note that you need -- before -foo in this case. nud Turn on or off NUD (Neighbor Unreachability Detection) on the interface. NUD is usually turned on by default. accept_rtadv Specify whether or not to accept Router Advertisement messages received on the interface. Note that the kernel does not accept Router Advertisement messages, even if the flag accept_rtadv is on, unless either the net.inet6.ip6.accept_rtadv vari- able is non-0, or the flag override_rtadv is on. This flag is set to 1 by default. override_rtadv Specify whether or not to override the net.inet6.ip6.accept_rtadv variable. If the flag is on, then it will suffice to set the flag accept_rtadv to make the kernel accept Router Advertisement messages on the interface. This flag is set to 0 by default. prefer_source Prefer addresses on the interface as candidates of the source address for outgoing packets. The default value of this flag is off. For more details about the entire algorithm of source address selection, see the IMPLEMENTATION file supplied with the KAME kit. disabled Disable IPv6 operation on the interface. When disabled, the interface discards any IPv6 packets received on or being sent to the interface. In the sending case, an error of ENETDOWN will be returned to the application. This flag is typically set automatically in the kernel as a result of a certain failure of Duplicate Address Detection. While the flag can be set or cleared by hand with the ndp command, it is not generally advisable to modify this flag manually. basereachable=(number) Specify the BaseReachbleTimer on the interface in millisecond. retrans=(number) Specify the RetransTimer on the interface in millisecond. curhlim=(number) Specify the Cur Hop Limit on the interface. -n Do not try to resolve numeric addresses to hostnames. -p Show prefix list. -P Flush all the entries in the prefix list. -r Show default router list. -R Flush all the entries in the default router list. -s Register an NDP entry for a node. The entry will be permanent unless the word temp is given in the command. If the word proxy is given, this system will act as a proxy NDP server, responding to requests for hostname even though the host address is not its own. -t Print timestamp on each entry, making it possible to merge output with tcpdump(8). Most useful when used with -A. RETURN VALUES
The ndp command will exit with 0 on success, and non-zero on errors. SEE ALSO
arp(8) HISTORY
The ndp command first appeared in the WIDE Hydrangea IPv6 protocol stack kit. BSD
November 6, 2009 BSD
All times are GMT -4. The time now is 07:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy