edit fields awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting edit fields awk
# 1  
Old 06-09-2010
edit fields awk

Hi there, i need some help please...
I have this text, it's name data.txt that contains the following information:
Code:
Mark Owen: 6999999888 6999999888 +302310999999 2310999999
Steve Blade Pit: +30691111222 2310888777 6999999888  
John Rose: 2310777555 310544565 +302310999999
Mary Stuart: 6999999888 6999999888 6999999888 310544565
George Other One: 6999999

888

Names and their telefone numbers.
Some numbers do not start with +30, I want every number to start with +30 (because it's a code) using sed, grep, awk.

Please, help me...

Last edited by Franklin52; 06-09-2010 at 02:40 PM.. Reason: Please use code tags
# 2  
Old 06-09-2010
Code:
sed "s/ \([0-9]\{1,\}\)/ +30\1/g" file

# 3  
Old 06-09-2010
A small variant one with sed:

Code:
 
sed 's/ \([0-9][0-9]*\)/ +30\1/g' input_file

# 4  
Old 06-09-2010
Thanks guys!
It works ! Smilie Smilie
# 5  
Old 06-09-2010
Quote:
Originally Posted by Mark_orig
Thanks guys!
It works ! Smilie Smilie
An awk version too..Which i like Smilie

Code:
 
awk '{ for (i=1;i<=NF;i++) { ($i  ~ /[0-9]/)&&($i !~ /^\+30/)?$i="+30"$i:$i}}1' input_file

# 6  
Old 06-09-2010
@panyam,
I couldn't understand the last part of your awk :

Quote:
?$i="+30"$i:$i
Could you please explain??
# 7  
Old 06-10-2010
Quote:
Originally Posted by gvj
@panyam,
I couldn't understand the last part of your awk :

Could you please explain??
Sure,

If the field is a number and does not start with +30: (true) append to the field the value +30: (false) put the number as it is
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

2. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

3. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

4. Shell Programming and Scripting

Join fields comparing 4 fields using awk

Hi All, I am looking for an awk script to do the following Join the fields together only if the first 4 fields are same. Can it be done with join function in awk?? a,b,c,d,8,,, a,b,c,d,,7,, a,b,c,d,,,9, a,b,p,e,8,,, a.b,p,e,,9,, a,b,p,z,,,,9 a,b,p,z,,8,, desired output: ... (1 Reply)
Discussion started by: aksijain
1 Replies

5. Shell Programming and Scripting

need to store query output fields in variables edit them and update the same in tables.

Hi , I have a query like select err_qty,drop_qty,unbld_qty,orig_qty from usage_data; I need to store the values of these fetched fields in variables, Need to edit them and update the new values into the table. Can anyone please help me in writing this piece of code:( (1 Reply)
Discussion started by: Rajesh Putnala
1 Replies

6. Shell Programming and Scripting

Edit a file using awk ?

Hey guys, I'm trying to learn a bit of awk/sed and I'm using different sites to learn it from, and i think I'm starting to get confused (doesn't take much!). Anyway, say I have a csv file which has something along the lines of the following in it:"test","127.0.0.1","startup... (6 Replies)
Discussion started by: jimbob01
6 Replies

7. Shell Programming and Scripting

use awk to edit a file..pls help

hey i want to over write the fourth field of a ':' delimited file by first finding the required row by using grep. i have done the following cat file | grep no. | awk -F ':' { $4=count; print $1:$2:$3:$4;} the correct values are being printed but nothin is bein added to the file..please... (5 Replies)
Discussion started by: dhe.arora
5 Replies

8. Shell Programming and Scripting

search and edit in the same file using awk

Hi, I am having a user.txt contains the name of users and passwd.txt file contains as passwd.txt $cat usr.txt root bin daemon cap $cat passwd.txt root:x:0:0:root:/root:/usr/bin/ksh bin:x:1:1:bin:/bin:/sbin/csh daemon:x:2:2:daemon:/sbin:/usr/bin/ksh adm:x:3:4:adm:/var/adm:/sbin/nologin... (4 Replies)
Discussion started by: Manabhanjan
4 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

10. Shell Programming and Scripting

sed command to edit fields

Hi, I'm a newbie to sed and I'm having trouble working with sed and fields. Suppose I have a text file with: AAA RFG:$2.10:6:25Oct06 WDD GGTR:$3.50:5:25Oct06 ADDSJ OO:$1.37:3:26Oct07 UGBDN S:$4.73:1:27Oct06 USY ADC:$2.38:20:27Oct06 And I want to substitute field 2 of line 3 with, say,... (3 Replies)
Discussion started by: aloe_vera
3 Replies
Login or Register to Ask a Question