0

I need to sum the number of lines from different text files like: x_red, x_green which have some information inside of them, and this is what I got:

counter=0

colors=`cat manyColors.txt`

addPrefix(){
   echo "x_$1"
}


for color in colors
do
#   cat `addPrefix $color` | wc -l  # will give me the nº of lines just fine
   counter=$(($counter + cat `addPrefix $color` | wc -l ))
done 

So yeah, the counter isn't really incrementing properly and I have no ideia how to sum those arguments properly, what am I missing here?

Edit: Also, how can I turn " cat addPrefix $color|wc -l ", into a variable?

Edit2: Added the function and changed the function name, for misunderstandings.. Sorry I know it's still kinda confusing

jruivo
  • 447
  • 1
  • 8
  • 22
  • Also what is `changeColor $color`? Is it come command? – anubhava Oct 09 '14 at 15:50
  • It's a function which will return the name of a text file, sorry about the confusion , just came up with some weird names. It'll just add a prefix to the input argument like: changeColor red --> x_red , which is an existing text file in the directory. – jruivo Oct 09 '14 at 15:55

3 Answers3

0

Instead of your counter= line you can use this code in BASH inside your for loop:

(( counter += $(wc -l < $(changeColor $color)) ))

There is no need to use cat here.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • First of all, thank you very much for the reply, Im sorry for being kind of stubborn with this but Im still curious, is there a way to use cat on this situation? – jruivo Oct 09 '14 at 16:06
  • 1
    And why do you want to use useless `cat`? – anubhava Oct 09 '14 at 16:07
  • If you really want it the use: `(( counter += $( cat $(changeColor $color) | wc -l) ))` – anubhava Oct 09 '14 at 16:07
0

When you are using $(...) syntax, you do not need to reference your inner variable with a $.

You should be able to simply use

    COUNTER=0
    for color in colors
    do
        COUNTER=$((COUNTER + $(cat $(changeColor $color) | wc -l`)))
    done

Be sure to embed your cat function within the $(command) since you cannot use nested backticks.

Murphy4
  • 1,499
  • 2
  • 15
  • 22
  • Thank you and thank you anubhava for the help. Just a quick question, can I eventually turn "cat `changeColor $color` | wc -l" into a variable? Is it doable? – jruivo Oct 09 '14 at 16:18
  • I don't quite understand what you are asking. If you mean inside the for loop for readability you definitely can. I would use the local keyword http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-5.html. Alternately you can create a function that takes a color and outputs the file name. – Murphy4 Oct 09 '14 at 16:23
0

I find your question unintelligible, but if, as your title suggests, you want to sum the lines in multiple files, you can do that quite easily like this:

file1:

line 1
line 2
line 3

file2:

line 1
line 2

cat file[12] | wc -l
5

The trick is not to pass the filenames to wc but just pass all the files as a single stream on its stdin (by using cat) then it cannot count, and give totals for each individual file. Compare this:

wc -l file*
   3 file1
   2 file2
   5 total
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432