My input files are a single column with variable length of rows. My code is supposed to count those rows, and print a specified number. The problem I'm having, is if the number of rows captured in the variable "A" is less than 1, I want it to print at least 1 row so the output file isn't empty. If "A" (1% of the total number of rows) is greater than 1, I want to print that number of rows. My hybrid awk-bash code looks like this:
#!/bin/sh
for i in {1..2}
do
input="../ExpressionSet_"$i"_chunk.txt"
for j in {1..2}
do
A=$(awk 'END{print NR*0.01}' $input)
Y=1
X=0
if (( "$A" -lt "$Y")); then
X=$A+1
else
X=$A
fi
awk 'NR<='$X' {print $0}' $input > "$i"_top1pc.txt
B=$(awk 'END{print NR*0.05}' $input)
awk 'NR<='$B' {print $0}' $input > "$i"_top5pc.txt
Confusingly, I keep getting error messages like
thresholdSelector_pc.sh: line 20: ((: 0.24 -lt 1: syntax error: invalid arithmetic operator (error token is ".24 -lt 1")
thresholdSelector_pc.sh: line 20: ((: 47.24 -lt 1: syntax error: invalid arithmetic operator (error token is ".24 -lt 1")
By the way, inputFile1 has 24 lines and inputFile2 has 4724 lines. Thanks for the help!
-lt
isn't a valid arithmetic operator inside(( ... ))
- and bash only performs integer arithmetic anyhow. Can you not do what you want within awk?