adding a number with sed or awk.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding a number with sed or awk.
# 1  
Old 08-06-2012
adding a number with sed or awk.

Hi.. I have this delicate problem..Smilie I have this huge ldif file with entry's like this example below..

And I need to change the following entrys.

Code:
telephoneNumber:
emNotifNumber:
billingnumber=
BillingNumber:

Al these entrys has a number like 012345678 and it needs to add one more number in begining,, like 9012345678

So
telephoneNumber:012345678 needs to be change to telephoneNumber:9012345678 and so on..

this is a huge file so I would like this to run in one go..
So... Smilie How would I do this?

Code:
# entry-id: 192
dn: uniqueidentifier=um704,ou=C10,ou=site1,o=users
nsUniqueId: a7e6a34b-1dd111b2-80f1bd80-559f9638
modifyTimestamp: 20090617001511Z
modifiersName: uniqueidentifier=um9999999999999999,o=users
mailHost: mail.sugar.pop.us
mailAlternateAddress: 012345678@sugar.pop.us
emFTL: -1,F
BadLoginCount: 0
umpassword: password
Password: password
mailForwardingAddress: 123123@sugar.pop.com
cn: SUGAR POP
sn: SUGAR
givenName: POP
uniqueIdentifier: um704
uid: 012345678
telephoneNumber: 012345678
mailQuota: 7549747
mail: 012345678@sugar.pop.com
emNotifNumber: 012345678
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
objectClass: nsmessagingserveruser
objectClass: inetmailuser
objectClass: inetlocalmailrecipient
objectClass: LIP
objectClass: emuser
objectClass: emNotification
gender: F
l: 0
preferredLanguage: en
emMessagecharencoding: ISO-8859-1
mailDeliveryOption: mailbox
mailDeliveryOption: forward
emSMPPcenterid: SMSC
emPreferredDateFormat: yyyy/mm/dd
emPreferredTimeFormat: 24
userPassword: userpassword
creatorsName: cn=directory manager
createTimestamp: 20031201054612Z
emServiceDn: cos=20,ou=C10,ou=site1,o=users

# entry-id: 193
dn: billingnumber=012345678,uniqueIdentifier=um704,ou=C10,ou=site1,o=users
nsUniqueId: a7e6a34c-1dd111b2-80f1bd80-559f9638
modifyTimestamp: 20090617001511Z
modifiersName: uniqueidentifier=um9999999999999999,o=users
admininfo: uniqueidentifier=um1p383
BillingNumber: 012345678
AnsweringService: LM
objectClass: top
objectClass: confmsgbox
subscribertimezone: bluemoon/mars
creatorsName: cn=directory manager
createTimestamp: 20031201054612Z
ActiveGreetingId: SpokenName,AllCalls
COSDN: cos=20, ou=C10, ou=site1,o=users

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 08-06-2012 at 07:17 PM.. Reason: code tags, please!
# 2  
Old 08-06-2012
How about this:

Code:
awk -F": " -v prefix=9 -v vals=telephoneNumber,emNotifNumber,billingnumber,BillingNumber '
    BEGIN{OFS=": ";split(vals,tmp,","); for(val in tmp) V[tmp[val]]}
    $1 in V { $2=prefix $2} 1' infile

# 3  
Old 08-07-2012
highligh the billing entry

Yes.. That almost works.. Smilie

Code:
# entry-id: 192
dn: uniqueidentifier=um704,ou=C10,ou=site1,o=users
nsUniqueId: a7e6a34b-1dd111b2-80f1bd80-559f9638
modifyTimestamp: 20090617001511Z
modifiersName: uniqueidentifier=um9999999999999999,o=users
mailHost: mail.sugar.pop.us
mailAlternateAddress: 012345678@sugar.pop.us
emFTL: -1,F
BadLoginCount: 0
umpassword: password
Password: password
mailForwardingAddress: 123123@sugar.pop.com
cn: SUGAR POP
sn: SUGAR
givenName: POP
uniqueIdentifier: um704
uid: 012345678
telephoneNumber: 9012345678
mailQuota: 7549747
mail: 012345678@sugar.pop.com
emNotifNumber: 9012345678
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
objectClass: nsmessagingserveruser
objectClass: inetmailuser
objectClass: inetlocalmailrecipient
objectClass: LIP
objectClass: emuser
objectClass: emNotification
gender: F
l: 0
preferredLanguage: en
emMessagecharencoding: ISO-8859-1
mailDeliveryOption: mailbox
mailDeliveryOption: forward
emSMPPcenterid: SMSC
emPreferredDateFormat: yyyy/mm/dd
emPreferredTimeFormat: 24
userPassword: userpassword
creatorsName: cn=directory manager
createTimestamp: 20031201054612Z
emServiceDn: cos=20,ou=C10,ou=site1,o=users

# entry-id: 193
dn: billingnumber=012345678,uniqueIdentifier=um704,ou=C10,ou=site1,o=users
nsUniqueId: a7e6a34c-1dd111b2-80f1bd80-559f9638
modifyTimestamp: 20090617001511Z
modifiersName: uniqueidentifier=um9999999999999999,o=users
admininfo: uniqueidentifier=um1p383
BillingNumber: 9012345678
AnsweringService: LM
objectClass: top
objectClass: confmsgbox
subscribertimezone: bluemoon/mars
creatorsName: cn=directory manager
createTimestamp: 20031201054612Z
ActiveGreetingId: SpokenName,AllCalls
COSDN: cos=20, ou=C10, ou=site1,o=users

The dn entry for billing number did not change..

Last edited by pelama; 08-07-2012 at 02:48 AM.. Reason: highligh the billing entry
# 4  
Old 08-07-2012
Not quite as elegant, but I kept the flexibility of the original solution:

Code:
awk -F": " -v add=9 -v vals=telphoneNumber,emNotifNumber,billingnumber,BillingNumber '
    BEGIN{OFS=": ";split(vals,tmp,","); for(val in tmp) V[tmp[val]]}
    $1 in V { $2=add $2}
    /=/ {for(str in V) gsub(str"=", str"="add)} 1' infile


Last edited by Chubler_XL; 08-07-2012 at 09:56 PM..
# 5  
Old 08-08-2012
An other issue,,

SmilieThis worked as Charm in my mac,Smilie

Then I lifted it out to my Solaris10, Smilie
and because this is way over my head now,, I don't know what went wrong..

Code:
 awk -F ": " -v add=9 -v vals=telephoneNumber,emNotifNumber,billingnumber,BillingNumber '
    BEGIN{OFS=": ";split(vals,tmp,","); for(val in tmp) V[tmp[val]]}
    $1 in V { $2=add $2}
    /=/ {for(str in V) gsub(str"=", str"="add)} 1' userdb
awk: syntax error near line 1
awk: bailing out near line 1
awk -F ": " -v add=9 -v   userdb  0.00s user 0.00s system 0% cpu 0.009 total

I'm really glad you helping me out here Chubler Smilie Smilie
# 6  
Old 08-08-2012
Use nawk in solaris.
# 7  
Old 08-08-2012
nawk. Still give me same problem

Code:
# time nawk -F": " -v add=9 -v vals=telephoneNumber,emNotifNumber,billingnumber,BillingNumber '
    BEGIN{OFS=": ";split(vals,tmp,","); for(val in tmp) V[tmp[val]]}
    $1 in V { $2=add $2}
    /=/ {for(str in V) gsub(str"=", str"="add)} 1' userdb
nawk: syntax error at source line 4
 context is
            >>>  /= <<<
nawk: bailing out at source line 4
nawk -F": " -v add=9 -v   userdb  0.00s user 0.00s system 0% cpu 0.009 total

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding text from a variable using sed (Or awk) with punctuation

Hi All, I would have though this would have been simple, but... I have text in a variable that I need to insert into a bunch of other files... The text is simple: ... (2 Replies)
Discussion started by: joeg1484
2 Replies

2. Shell Programming and Scripting

adding number based on pattern using awk

Hi, I want to add numbers based on pattern . in the below ouput file , i want number to be added after pattern line ---- , ie 1 and next is also 1 and next is 2 and next is 3 after adding 4 numbers say output is 7 should be redirected to another file . like this it should add 4 digits after... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

3. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

4. Linux

Adding a prefix to a column using awk/sed commands

Hello, I am a newbie to linux and struggling to find a better way to append a column in a text file. Here is the file i want to modify: It has 8 columns (and thousands of rows). I want to append the first column by adding "chr" infront of the numbers. Some rows have a string in the first... (4 Replies)
Discussion started by: bjorngill
4 Replies

5. UNIX for Dummies Questions & Answers

Adding a column with the row number using awk

Is there anyway to use awk to add a first column to my data that automatically goes from 1 to n , where n is the numbers of my rows?:confused: (4 Replies)
Discussion started by: cosmologist
4 Replies

6. Emergency UNIX and Linux Support

Adding carriage returns to file using sed/awk

Hello, I need help adding carriage returns at specific intervals (say 692 characters) to a text file that's one continous string. I'm working in AIX5.3. Any quick help is appreciated. Thanks! (2 Replies)
Discussion started by: bd_joy
2 Replies

7. Shell Programming and Scripting

sed/awk-adding numeric to a column

I have a txt file as follows Code: Oct 1 file1 4144 Oct 1 file23 5170 Oct 2 file5 3434 Oct 21 file56 2343 I need to add a new column by marking the right log file from current directory. For example populate like this. Please not in the second columt for "1" it has... (2 Replies)
Discussion started by: gubbu
2 Replies

8. Shell Programming and Scripting

awk/sed - getting string instead of number

Hi! I am writing a script handling downloading list of files and I have to check whether file is present locally and if not finished than continue downloading. To do so I have to compare sizes of remote file and local file. To check remote file size I have to parse something like this: ... (2 Replies)
Discussion started by: hrwath
2 Replies

9. UNIX for Dummies Questions & Answers

Adding a new field using sed or awk?

I have a bcp file that contains 10 fields. These fields are separated by a tab. How can I add my name as a new field in the 8th position for every record? I've been playing w/ sed and awk but can't seem to figure this out. (3 Replies)
Discussion started by: sasabune
3 Replies

10. UNIX for Dummies Questions & Answers

Truncating a number using sed/awk

Hi all, I'm trying to truncate a number like the following: 0001060407013900501048239559900600504083525826350002050354795057 I would like to create an output which puts carriage returns every so many characters, giving an output such as: 0001 060407 0139 0 05 010482395599 0060... (4 Replies)
Discussion started by: michaeltravisuk
4 Replies
Login or Register to Ask a Question