Specifying and replacing fields with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Specifying and replacing fields with awk
# 1  
Old 11-18-2008
Specifying and replacing fields with awk

#cat BATCH007.TXT
01,661060052,061000104,081118,0915,07,80,1,2/
99,,,2/

I have this file called BATCH007.TXT. I am trying to change fields 2 and 3 on line 2 to have zeroes. Like this:
01,661060052,061000104,081118,0915,07,80,1,2/
99,0,0,2/

I can use these commands to print identify the fields, which return a empty value, as they should:
head -2 BATCH007.TXT | tail -1l | awk '{FS=","} {print $2}'
head -2 BATCH007.TXT | tail -1l | awk '{FS=","} {print $3}'

I think I'm close. I added a gusb to the above command, but the output is incorrect:

head -2 BATCH007.TXT | tail -1l | awk -F "," '{gsub( $2,"0");print}' BATCH007.TXT > D.new

$cat D.new
01,661060052,0,081118,0915,07,80,1,2/
09090,0,0,020/0

Does anyone have any suggestions?
# 2  
Old 11-18-2008
looks like homework to me
# 3  
Old 11-19-2008
Hi,

i don't know exactly what you are trying to do, but if you only want to change line 2 of you file, this should be enough:

Code:
sed "/^99/s/,,,/,0,0,/g" file

Which means: go to the line starting with 99 and on this line substitute three kommas in row by ,0,0,

HTH Chris
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

Swapping/replacing fields

Hallo Team, I would like to replace filed 4 and 7 with filed 39 how can i achieve this ? -bash-3.2$ cat dip1.csv| cut -f4,7,24,36,39 -d","|sort -u +27113996891,+27113996891,196.35.130.52,828854047,+27873500077 +27116452690,+27825702918,10.0.109.13:5060,+27116452690,+27116452690... (2 Replies)
Discussion started by: kekanap
2 Replies

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

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

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

6. Shell Programming and Scripting

Replacing certain fields from certain rows

Hi all, say for example i have the next input file 30 Au 7.500000 7.500000 5.774000 Au 7.500000 8.995000 8.363000 Au 7.500000 6.005000 8.363000 Au 20.633000 7.500000 9.226000 Au 20.632000 6.005000 6.637000 Au 20.632000 ... (4 Replies)
Discussion started by: ezitoc
4 Replies

7. Shell Programming and Scripting

Replacing fields

Hi! I have a file somefile.txt: 12, 1, a, b, c, d, e, f 12, 1, a, b, c, d, e, f 17, 51, a, b, c, d, e, f ... I've made this script to read two fields from a line and output a third: cat somefile.txt | awk -F, '{if ($1 == "12" && $2== "1") print "19"; else if ($1 == "17" && $2== "51")... (8 Replies)
Discussion started by: Tr0cken
8 Replies

8. Shell Programming and Scripting

Comparing two files and replacing fields

I have two files with ids and email addresses. File 2 cotains a subset of the records in file 1. The key field is the first field containing the id. file 1: 123|myadr@abc.com 456|myadr2@abc.com 789|myadr3@abc.com file 2: 456|adr456@xyz.com Where the record appears in the second... (3 Replies)
Discussion started by: tltroy
3 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. UNIX for Dummies Questions & Answers

Finding & Replacing specific Fields

All I have a very large file (aproximately 150,000) as shown below separated by pipe "|". I need to replace data in 2, 16, 17, 23 fields that are of time stamp format. My goal is to look in those fields and it ends with "000000|" then replace it with "000|". In other words, make it as 6 digit... (2 Replies)
Discussion started by: ddraj2015
2 Replies
Login or Register to Ask a Question