else condition in awk
3
0
Entering edit mode
7.7 years ago
sinumolg ▴ 10

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 • 2.8k views
ADD COMMENT
2
Entering edit mode
7.7 years ago
5heikki 11k

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
ADD COMMENT
0
Entering edit mode

thanks

but its not working fine

ADD REPLY
1
Entering edit mode

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

ADD REPLY
1
Entering edit mode
7.7 years ago
LLTommy ★ 1.2k

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

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
7.7 years ago
Rohit ★ 1.5k

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
ADD COMMENT

Login before adding your answer.

Traffic: 1540 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6