For loop error

Hi,

I am new to shell scripting and trying three for loops to filter the data. So my data is like

INPUT
cat /tmp/filename.txt

name service machine number of service
sh1 service1 machine1 3
sh3 proces2 machine5 5
sh16 service15 machine24 1
sh1 service1 machine4 3
sh16 service15 machine1 1
sh1 service15 machine2 1

and I want to get it to print as

OUTPUT

     service1      |    service16

name 1-2-3-4-5-6 | 1-2-3-4-5-6
----------------------------------------
sh1 0-0-2-0-0-0 , 1-0-0-0-0-0
sh16 0-0-0-0-0-0, 2-0-0-0-0-0

for this I wrote a script like

cat /tmp/filename.txt | awk '{print $1}' | sort | uniq > /tmp/uniqlist.txt
for i in `cat /tmp/uniqlist.txt`

do
for j in "service1" "service15"
do
for k in {1..6}
do
test_[$i]_[$j]_[$k] =cat /tmp/filename.txt | grep $i | grep $j | grep $k | wc -l
echo test_[$i]_[$j]_[$k] >> /tmp/output.txt
done
done
done

It's printing the value on screen but saying test_[sh1]_[service1]_[1]=0 : command not found and the outfile is totally empty.

Difficult ... where to start?
Well, this is what (including some inconsistencies) I get from your code snippet after having eliminated the gravest syntax problems:

$ cat /tmp/output.txt
test_[name]_[service1]_[1]
.
.
.
test_[name]_[service15]_[6]
test_[sh1]_[service1]_[1]
.
.
.
test_[sh1]_[service1]_[6]
test_[sh1]_[service15]_[1]
.
.
.
test_[sh1]_[service15]_[6]
test_[sh16]_[service1]_[1]

The largest problem seems to be the test_[$i]_[$j]_[$k] =cat /tmp/filename.txt | grep $i | grep $j | grep $k | wc -l :

  • that ...[...]...[...]... is not a construct known to me, at least in the most commen shells. BTW, what's your shell? You forgot to mention...
  • (common) shells don't like a space around the = . That's the reason for your error msg.
  • the cat ... pipe needs to be run in a "command substitution" $(...) (c.f. man bash or your shell's man page)

You seem to have missed the "expansion" of the variable in the echo statement.
I don't see any relation to your output sample from the structure of your script. And, there's no service16 mentioned anywhere except in your output.

Aside: be aware of "false positives" (like sh1 matching sh1 AND sh16).

sorry for not mentioning. I'm using bash.
also apologies for the errors in the code. without space and adding $ in the cat also not helping much.

cat /tmp/filename.txt | awk '{print $1}' | sort | uniq > /tmp/uniqlist.txt
for i in `cat /tmp/uniqlist.txt`
     do
         for j in "service1" "service16"
             do
                 for k in {1..6}
                     do
                         test_[$i]_[$j]_[$k]=$(cat /tmp/filename.txt | grep $i | grep $j | grep $k | wc -l)
                 done
        done
        echo -e $i, ${test_[$i]_[service1]_[1]}"-"${test_[$i]_[service1]_[2]}"-"${test_[$i]_[service1]_[3]}"-"${test_[$i]_[service1]_[4]}"-"${test_[$i]_[service1]_[5]}"-"${test_[$i]_[service1]_[6]}   ${test_[$i]_[service16]_[1]}"-"${test_[$i]_[service16]_[2]}"-"${test_[$i]_[service16]_[3]}"-"${test_[$i]_[service16]_[4]}"-"${test_[$i]_[service16]_[5]}"-"${test_[$i]_[service16]_[6]}     >> /tmp/output.txt
done
cat /tmp/output.txt

You seem to have missed the "expansion" of the variable in the echo statement?. Can you help me on this. I even tried printf but same result, or is there any other way I can get this output printed in a file.

As said before, that test_[$i]_[$j]_[$k] is a construct unknown (to me, at least). What exactly do you want to achieve?

I want to show the file(tmp/filename.txt) based on memory used (3gb,1gb,2gb which is mentioned at last of the file) for each service(service1,service16) on given projects(sh1,sh16 etc). so for each project the memory used by the service name will be different and I want to take a count from the input file like below.

OUTPUT

         service1      |  service16
name 1-2-3-4-5-6 | 1-2-3-4-5-6
---------------------------------------- 
sh1 0-0-2-0-0-0 , 1-0-0-0-0-0
sh16 0-0-0-0-0-0, 2-0-0-0-0-0