loop through a list without array in shell

0

I'm trying to write a short shell script to loop through a list (given by a certain command I run) line by line and push values to CSV file

a list for example:

aaa   bbb   ccc   ddd   eee   fff
jjj   kkk   lll   mmm   nnn   ooo
uuu   vvv   www   xxx   yyy   zzz

Need to loop through this and for each line take 1st,4rd and 6th fields into variables so I can do some manipulation on them.

#!/bin/sh
#
CSVfile="/tmp/`hostname`.csv"
filter=$( command )
touch $CSVfile
#
for i in $filter
do 

    first=$( printf "$i" | awk {'print$1'})
    fourth=$( printf "$i" | awk {'print$4'})
    sixth=$( printf "$i" | awk {'print$6'})
    cmd=$( cat /home/app/$first/$fourth/out | grep value | awk -F: {'print$2'})
    echo `date +%d%m%Y,%H%M%S`,"$sixth","$cmd" >> $CSVfile

done

the problem here is the for loop goes field by field and not line by line. need each line goes into $i so I can parse it with "awk"

Thanks

Skater000

Posted 2020-02-08T12:26:15.093

Reputation: 1

Answers

0

This has been resolved by using while loop with IFS which gets filter variable as input.

Could not find a why to push command into while loop w/o store it in variable.

#!/bin/sh
#
CSVfile="/tmp/`hostname`.csv"
filter=$( command )
touch $CSVfile
#
while IFS= read -r line
do
    first=$( printf "$i" | awk {'print$1'})
    fourth=$( printf "$i" | awk {'print$4'})
    sixth=$( printf "$i" | awk {'print$6'})
    cmd=$( cat /home/app/$first/$fourth/out | grep value | awk -F: {'print$2'})
    echo `date +%d%m%Y,%H%M%S`,"$sixth","$cmd" >> $CSVfile

done < $filter

Skater000

Posted 2020-02-08T12:26:15.093

Reputation: 1