I have a CSV File and I want to have multiple checks to retrieve the data from the CSV file. I have the following conditions which needs to be taken care of and I am trying to use awk in a shell script to fulfill these conditions:
if ( ColD == "TRX" || ColD == "TX" )
{
if (ColJ == "BTS INT UNAFF" || ColJ == "LOCAL MODE")
{
if (ColL !="OPER" && ColL != "") #Second Value shouldn't be blank
{
if (ColU != "2000")
{
if (ColA != "*_*") # Should not contain _ in the value.
then
print all the resulting filtered columns of this csv file
Here ColA, ColB etc. are the column numbers which i use as $1, $2 and so on.
Now I know simple awk usage of a single if condition is something like this:
awk -F, '{
if ($23 == "TOP36")
print $11
}' $INPUT_PATH/$fileName
and could find the nested if conditions something like this:
awk '
{if ( $6 == 0 && $8 > 0 && $8 <= 9 ) { $6 = "left" } else {
if ( $8 == 0 && $6 > 0 && $6 <= 15 ) { $6 = "bottom" } else {
if ( $8 == 9 && $6 > 0 && $6 <= 15 { $6 = "top" } else {
if ( $6 == 15 && $8 > 0 && $8 <= 9 { $6 = "right" }
}
}
}
} {print}'
But, I think my conditions can be satisfied if I put satisfy one if condition on top and then next under it and so on. How can I do it using awk if statements? Is there a better way to do this?
Best Answer
Nested
if
statements are effectively statementand
statement, so if you do not need to do any particular processing as you step through the nesting, then you can just join them all up with&&
.To define your Field Separator, you can use option
-F,
instead of parameterFS=,
if you prefer – or have it fully contained within theawk
code, in theBEGIN{ }
pre-file-processing block:BEGIN{ FS="," }