I'm trying to write a small script that will count entries in a log file, and I'm incrementing a variable (USCOUNTER
) which I'm trying to use after the loop is done.
But at that moment USCOUNTER
looks to be 0 instead of the actual value. Any idea what I'm doing wrong? Thanks!
FILE=$1
tail -n10 mylog > $FILE
USCOUNTER=0
cat $FILE | while read line; do
country=$(echo "$line" | cut -d' ' -f1)
if [ "US" = "$country" ]; then
USCOUNTER=`expr $USCOUNTER + 1`
echo "US counter $USCOUNTER"
fi
done
echo "final $USCOUNTER"
It outputs:
US counter 1
US counter 2
US counter 3
..
final 0
You are using
USCOUNTER
in a subshell, that's why the variable is not showing in the main shell.Instead of
cat FILE | while ...
, do just awhile ... done < $FILE
. This way, you avoid the common problem of I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?:Note I also replaced the `` expression with a $().
I also replaced
while read line; do country=$(echo "$line" | cut -d' ' -f1)
withwhile read country _
. This allows you to saywhile read var1 var2 ... varN
wherevar1
contains the first word in the line,$var2
and so on, until$varN
containing the remaining content.Using the following 1 line command for changing many files name in linux using phrase specificity:
For all files with the extension ".jpg", if they contain the string "holiday", replace it with "honeymoon". For instance, this command would rename the file "ourholiday001.jpg" to "ourhoneymoon001.jpg".
This example also illustrates how to use the find command to send a list of files (-type f) with the extension .jpg (-name '*.jpg') to rename via a pipe (|). rename then reads its file list from standard input.
Incrementing a variable can be done like that:
Counting the number of occurrence of a pattern in a column can be done with grep
-c
count([^ ]* )
To detect a colonne{2}
the colonne numberUS
your pattern-r
with read.cut
, you can stick with pure bash solutions.read
a 2nd var (_
) to catch the additional "fields"[[ ]]
over[ ]
.I had the same $count variable in a while loop getting lost issue.
@fedorqui's answer (and a few others) are accurate answers to the actual question: the sub-shell is indeed the problem.
But it lead me to another issue: I wasn't piping a file content... but the output of a series of pipes & greps...
my erroring sample code:
and my fix thanks to the help of this thread and the process substitution: