mercredi 27 février 2019

BASH - loop (for) with IF thru each line from file

I Have file (halted.txt) with data like below

IMPORT_FRP_DRAWDOWN_MQ,1
eFXO_IMPORT_RFQ_MQ,1
IMPORT_FROM_MCDM,1
deal_export,1

and now the question is how to loop against this file and perform some action (add +1 to number of the ned) only ones for each lines and stop when number is 5 or >5

IFS=$'\n' # make newlines the only separator
set -f    # disable globbing

for p in $(cat < "halted.txt"); do
   if [[ $p == *"5"* ]]; then
       echo "There is 5 on the end", $p
   elif [[ $p > *"5"* ]]; then
       echo "add +1 till 5"
       awk -F, '{$2=$2+1}1' OFS=, halted.txt > temp && mv temp halted.txt
   fi
done

Currently every run's number 1 is increased not only ones but 4 times, because I have 4line inside file. then first run will give me

IMPORT_FRP_DRAWDOWN_MQ,4
eFXO_IMPORT_RFQ_MQ,4
IMPORT_FROM_MCDM,4
deal_export,4

next one 8 etc. How to make sure that as results only +1 will be added?

Aucun commentaire:

Enregistrer un commentaire