Hi I am new to programming.
While trying to execute the script below,
with an input file of this form (in this case they are called nohd*.txt
files)
NSGEAPKNFGLDVKITGESENDRDLGTAPGGTLNDIG
IIIIMMMMMOOOOOOOOOOOOOMMMMMMMMMMMIIII
I want to count the columns that have text from file d.txt
, see next.
D
O
to do this I wrote the following script
#!/bin/bash
for z in {1..141}
do
a=0
l=$(tail -1 nohd$z.txt | wc -m)
x=$(cat d.txt)
for ((p; p<=l; p++))
do
if [ "$(cut -c $p nohd$z.txt)" = "$x" ] ; then
a=$((a+1))
p=$((p+1))
fi
done
echo $a
done
I'm running this script using ./script
it shows this error:
cut: invalid byte/character position ‘{1..351}’
it turns correctly only the first value, for the rest of values it turns 0 the expected output is in the following form
5
20
4
Please, can anyone help me with this? Thank u
sh
? How are you executing the script? – terdon Oct 14 '19 at 16:54sh script.sh
?./script.sh
?bash script.sh
? Something else? Please [edit] your question and clarify. Also, please show your expected output as I requested before. – terdon Oct 14 '19 at 17:14grep
would suffice. (2) Maybe it's time to take somethig like Python, or Ruby, or Julia, or whatever higher-than-shell level language you have around. Complex string processing is not a shell's strong point. – 9000 Oct 14 '19 at 17:25grep
? I've tried it and it was unsuccessful – islem habibi Oct 14 '19 at 17:31p
should be1
for the first comparison withcut
, so try to change your loop tofor ((p=1; p<l; p++))
and you probably don't need to incrementp
withp=$((p+1))
(remove it). – Freddy Oct 14 '19 at 17:43