ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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??
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
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=","
}
FNR == NR {a[$1] = $2
b[$1] = $3
next
}
( $5 in a ){
if ( substr($1,2,5) == "JB007" )
$5 = b[$5]
else
$5 = a[$5]
}
FNR < NR' error_correction.txt input_file > output_file
Check it on your real example and look at the official manual suggested by druuna to correctly interpret this code.
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?
As you've read, it is an experimental feature which is not enabled by default in previous versions. If you want to try it, you have to compile gawk from source adding --enable-switch in the configure step or eventually update to a more recent version of gawk. Anyway, I don't think you really need it, unless your requirements changed again!
hey colicix,
guess what im back
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:
Code:
Code:
awk 'BEGIN{ FS=","; OFS=","
}
FNR == NR {a[$1] = $2
b[$1] = $3
next
}
( $5 in a ){ <-- what if there are multiple similar values?? like there are 3 entries of nos present in the error correction file along with an exrta column which is to matched with the data file?
if ( substr($1,2,5) == "JB007" )
$5 = b[$5]
else
$5 = a[$5]
}
FNR < NR' error_correction.txt input_file > output_file
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.
Well the questions are different it's just that i have used similar data.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.