Help needed in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed in awk
# 1  
Old 05-22-2006
Help needed in awk

Hi All,

I am trying to manipulate a file using awk in UNIX. My file "test_file" is structured as follows:

field1:field2:field3
field4:field5:field6
field7:field8:field9

My requirement is to calculate another field (which would be let's say sum of 2nd and third column and put as fourth column in this file). so that my final output would be

field1:field2:field3:fieldA
field4:field5:field6:fieldB
field7:field8:field9:fieldC

where fieldA=field2 + field3 and so on...

I have the following piece of code which I guess, is nowhere in the working condition right now:

while read line
do
echo $line | awk -F: 'BEGIN {OFS=":"}
{$4=$2 + $3 ; print $0}' >> new_test_file
done < test_file

I read somewhere that you can dynamically calculate variables in awk, so $4 would be dynamically calculated at the run time. Subsequently when i print $0 should contain the new calculated field too. But I guess that is not the case.

Can anybody look into above and help me out?

Thanks
# 2  
Old 05-23-2006
utterly confused !!!

Guyz,
Plz ignore my post. I just tried it on a different unix box and it worked. Both are korn shells. I have no idea why it did not work on the first one. I am going to do some further R&D on this.
Thanks anyways.
Vikas.
# 3  
Old 05-23-2006
Plz help me to understand this now

Guys

I finally resolved the problem. As i mentioned in my last post that somehow, this code was working on one box and giving prob on second one. Well, i try the same code with "nawk" on second one and it worked too.

Now, could someone plz explain why this happened? Can I continue to safely use nawk on my second server or is it not advisable. Appreciate any response, ppl.

Thanks
Vikas.
# 4  
Old 05-23-2006
I don't know anything about nawk, but your use of $4 seems wierd to me. $4 should be the 4th field in the record, but in your example there is no 4th field. Instead, you want to create the 4th field. Also, why are you bothering with a read?

I would skip the read and do something like --

awk -f myawk.awk myfile.txt > mynewfile.txt


Code:
# myawk.awk
BEGIN {
 FS=":"
 OFS=":"
}

{ 
 fld4 = $2 + $3
 print $0, fld4
}

END {}

# 5  
Old 05-23-2006
this is why

Hi Glenn
Thanks for your reply. However, $4 can definitly be calculated at the runtime. As I said i ran this piece of code on some other unix box and it ran fine. Also if i use nawk, it runs fine on the first one too.

Anyways, as I said, awk and nawk can dynamically calculate variables at run time. So even though i have $4 undefined, but i can calculate it at run time.
Secondly, i have to use read in a while loop, since apart from calculating a field like above, i am doing some other operations too. Lastly, $4 was just an example. My file could be a variable length file too in which case, i would need to use $NF to calculate the last field and then probably i can use $(NF+1) to calculate the new field and embed in the new file.

As far as nawk is concerned, i guess it stands for new awk. I read in some forums, that there is a difference in version of awk. So my best guess is my first box has an older version of awk whereas second one has a newer version. That is why i have to new awk or nawk on my first machine.

Thanks for your reply again
Vikas.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - help needed please

Hi.. have a file as below, appreciate if someone can help on this 143|500| 10| 23353 22131 23355 23354 23358 23352 23357 23350 23349 23351| RAID5 213|1008| 9| 22419 22412 221 22413 22414 22416 22417 22415 22418| RAID6 1088|500| 5| 22243 22240 22244 22242 22241| RAID5 322|1200| 12|... (6 Replies)
Discussion started by: richard0@rediff
6 Replies

2. Shell Programming and Scripting

awk help needed

Hi Everyone, i have following in my file 1 2 3 4 5 6 . . 100 and now i want the output as 1 4 7 ..........so on..............97 100 (10 Replies)
Discussion started by: zozoo
10 Replies

3. Shell Programming and Scripting

awk help needed

Hi Experts, I have a file (file 1) with several columns and I need to create 2 files based on the data of 20th column of file 1. Criteria 1 : If the 20th field of file1 is empty , copy the entire records to file 2. I am successfully able to do this with the following awk code : awk... (2 Replies)
Discussion started by: nua7
2 Replies

4. Shell Programming and Scripting

awk help needed

Hi, i have input records as shown below. 4097,Probe3,G10,255,05/17/2011 12:44:03:185,NULL,05/17/2011 12:39:03:180,05/17/2011... (1 Reply)
Discussion started by: raghavendra.nsn
1 Replies

5. UNIX for Dummies Questions & Answers

help needed for awk

Dear all, I am new to use unix. I run the following command and got the error. Anyone knows how should I modify the command. Thanks a lot! $ for chr in 'seq 1 23'; do awk 'BEGIN {print "T","pheno";}{print "M",$2}' out_${chr}.map > dat_${chr}.dat; done error message:... (2 Replies)
Discussion started by: forevertl
2 Replies

6. Shell Programming and Scripting

Awk Help needed

hi, I have input file woth records as shown below OCSMRC_OK,7057348733,+0.00,0,18/05/2010 23:42:19,BellMobility,302610000918553,0006056099,B30,686505,686505,OCS_MRC,+49.14,0,0 ,0,0, OCSPPKB_NOK,4163460120,+1.25,0,18/05/2010... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

7. UNIX for Dummies Questions & Answers

Awk help needed

I have a log file monitor script that checks through a log file for a string. I use awk to search the log file, starting at the last checked line, for the specified string and then output the count and the last row number checked. The part of the script that does all the work is here: set --... (6 Replies)
Discussion started by: mglenney
6 Replies

8. Shell Programming and Scripting

help needed in awk

Hi , i have a file a.txt like this: far near veryfar toonear typeset var1=veryfar to extract the text between two strings i use the following command : awk '/far/,$veryfar/' a.txt its not working can nyone tell pls whats wrong in it ? i doubt can we use variable in awk like this... (3 Replies)
Discussion started by: santosh1234
3 Replies

9. Shell Programming and Scripting

awk help needed

How do I alter this command so that it prints only the second comma delimited field from line number 3? Secondly, how do you redirect the output to a variable called TEST? Thanks (cat BATCH007.TXT | awk 'BEGIN { FS = "," } ; {print $2 }') (5 Replies)
Discussion started by: ddurden7
5 Replies

10. Shell Programming and Scripting

awk help needed

I am trying to write a script that will parse out the e-mail address of a person from the name of a file in a directory. Example: filename is: /home/myname/first.middle.last@email.com.xls I want to extract just the email address and mail the file to that address. I want to send the... (6 Replies)
Discussion started by: Drenhead
6 Replies
Login or Register to Ask a Question