using variables in awk
Hi i would like to add an extra dimension to a question i previously asked...
I'm not quiet sure as to how one uses different variable inside awk. summary: i have bad data for instance, the use of capital letters in the middle of the word. I identified the errors, made a list and put it in a file. Some errors are checked for a condition and then depending on the result the 2nd or 3rd value has to replace the actual value in the file. error_correction.txt Incorrect,Correct,Maybe VeNOM,Venom,Venemous nos,NOS,N2O . . . My data file looks like this: data.txt: vgr,bugatti veron,,3.5,Maybe,6,.......,.... vgr,lamborgini,,3.5,nos,6,.......,.... abc,bugatti veron,,3.5,N20,6,.......,....... . . . . I need to replace the terms in the 5th field with that from the list, after checking with an if condition whether to pick out the 2nd column or the 3rd column from the error_correction.txt file. How do i do this using awk?? Reference to previous question: http://www.linuxquestions.org/questi...-field-730433/ |
Please, show us the awk code you're using now. Also, what is the condition to choose the 2nd or the 3rd field from error_correction.txt?
|
currently this is the simplest form of the code: [http://www.linuxquestions.org/questi...field-730433/]
current code: awk -F"," 'FNR==NR{a[$1]=$2;next} ( $5 in a ){ $5=a[$5]; #This assigns the value from the 2nd column of the error_correction file }' error_correction file i want to add this condition to the awk code: { x=substr($1,2,5); } if ( x == "JB007" ) { #Assign the value from the 3rd column of the error_correction file } else { $5=a[$5];#Assign the value from the 2nd column of the error_correction file } I think that i may have to use another variable like b[$1]=$3 after the NR but i'm not quiet sure how to loop it up |
Is it possible to use functions in awk?
|
|
cool.....
n hey is it possible to use a switch case as well? i tried the --enable-switch but it didn't work.... |
Hi again,
From the same manual (!!): 6.4.5 The switch Statement You could have found that one yourself ;) |
yup :)
i saw it n tried it out but it didn't work :( it had something about it working only on version 3.1.3 for gawk. Do u know of any other way? |
If I correctly interpret your requirements (as explained in post #3 and in your previous thread) following the code posted by ghostdog74, this should do the trick:
Code:
awk 'BEGIN{ FS=","; OFS="," |
Quote:
|
hey colicix,
guess what im back :D hey im still not quiet familiar with awk! im not sure how to compare the values of two files and pick up various columns separately. In the code that you wrote, Quote:
|
any one around???
Code:
( $5 in a ){ |
knock knock
|
vgr, you have 3 threads running regarding the same awk problems, perhaps you should check your other threads for replies and ask new questions there.
|
Quote:
All i wanted to know was how to loop through repeated values in 2 separate columns present in two different files. What was happening in the case above was that it checked for the first occurrence only and then exited the loop :( |
All times are GMT -5. The time now is 11:31 AM. |