I am using if else statements in awk. Everytime when I use it I am getting a syntax error. Could anyone tell me how to use this? I am getting syntax error shown below.
Code:
awk '{ FS = "=" ;if($1 ~ /Hi/) {if (!($1=="Hi")) {print $1;}} else {if($1=="bye") {print $1;}}} else {if(if($1=="good") {print $1;}}} END {print $1}'
Output:
awk: { FS = "=" ;if($1 ~ /Hi/) {if (!($1=="Hi")) {print $1;}} else {if($1=="bye") {print $1;}}} else {if(if($1=="good") {print $1;}}} END {print $1}
awk: ^ syntax error
awk: { FS = "=" ;if($1 ~ /Hi/) {if (!($1=="Hi")) {print $1;}} else {if($1=="bye") {print $1;}}} else {if(if($1=="good") {print $1;}}} END {print $1}
awk: ^ syntax error
awk: { FS = "=" ;if($1 ~ /Hi/) {if (!($1=="Hi")) {print $1;}} else {if($1=="bye") {print $1;}}} else {if(if($1=="good") {print $1;}}} END {print $1}
awk: ^ syntax error
Problems
There are a lot of things wrong with your code. You have unbalanced braces, poor spacing, and other issues that make debugging hard. Ultimately, you have syntax and quoting problems.
Solutions
You can simplify your code by moving the assignment to FS outside your script. This is much more readable and less error-prone. For example:
You could also move your assignment to a BEGIN block for clarity, unless you are re-assigning it to something different at runtime.
No matter what else you do, if you refactor your code for readabilty, you will have a much easier time debugging your script.