This is a test version of Biostars. For the public version, visit https://www.biostars.org.
else condition in awk

Hi ,

I am trying to print the output of a set of 100 input files I want to generate two files 1. satisfying if condition 2. satisfying else condition

I am working in Bash . pls help me to solve this

for i in *.txt; do awk '{if(($NF>70) && ($3>30)) {print $1,$2,$3,$NF'} $i > output/$i.out; else $i > not/$i;} done;

i want to print output in to 'output' directory if it is satisfying if condition (($NF>70) && ($3>30)) and also i need an output file in 'not' directory as the result of else condition

While running this command its showing -bash: syntax error near unexpected token `else'

Thanks in Advance

r rna-seq sequencing blast next-gen

3 answers

Your title should be else condition in awk, not bash.

#!/bin/bash
mkdir output not
for i in $(find . -maxdepth 1 -type f -name "*.txt"); do
    awk -v O1="output/$i" -v O2="not/$i" \
    '{if(($NF>70) && ($3>30)){print $1,$2,$3,$NF > O1} else{print $0 > O2}}' $i
done

thanks

but its not working fine

If it's not working don't click 'accept' and tell us what the error is.

I think you miss the 'then' after your if condition. And you have to close your if-else-block with 'fi'. For further reference look e.g. here

Hi i tried this too. while doing then the error is like -bash: syntax error near unexpected token `else'

Stackoverflow may be a better place for scripting problems, also it might be easier if you give an example file.

for i in *.txt; do awk -v i="$i" '{ if(($NF>70) && ($3>30)) {print $1,$2,$3,$NF > "output/${i}.out"} else {print $0 > "not/${i}"} }'; done

Log in to answer this question.