Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

results of wc as variables

Tags:

bash

shell

wc

I would like to use the lines coming from 'wc' as variables. For example:

echo 'foo bar' > file.txt
echo 'blah blah blah' >> file.txt
wc file.txt

2  5 23 file.txt

I would like to have something like $lines, $words and $characters associated to the values 2, 5, and 23. How can I do that in bash?

like image 288
719016 Avatar asked Aug 19 '11 09:08

719016


5 Answers

In pure bash: (no awk)

a=($(wc file.txt))
lines=${a[0]}
words=${a[1]}
chars=${a[2]}

This works by using bash's arrays. a=(1 2 3) creates an array with elements 1, 2 and 3. We can then access separate elements with the ${a[indice]} syntax.

Alternative: (based on gonvaled solution)

read lines words chars <<< $(wc x)

Or in sh:

a=$(wc file.txt)
lines=$(echo $a|cut -d' ' -f1)
words=$(echo $a|cut -d' ' -f2)
chars=$(echo $a|cut -d' ' -f3)
like image 167
Arnaud Le Blanc Avatar answered Oct 14 '22 00:10

Arnaud Le Blanc


There are other solutions but a simple one which I usually use is to put the output of wc in a temporary file, and then read from there:

wc file.txt > xxx
read lines words characters filename < xxx 
echo "lines=$lines words=$words characters=$characters filename=$filename"
lines=2 words=5 characters=23 filename=file.txt

The advantage of this method is that you do not need to create several awk processes, one for each variable. The disadvantage is that you need a temporary file, which you should delete afterwards.

Be careful: this does not work:

wc file.txt | read lines words characters filename

The problem is that piping to read creates another process, and the variables are updated there, so they are not accessible in the calling shell.

Edit: adding solution by arnaud576875:

read lines words chars filename <<< $(wc x)

Works without writing to a file (and do not have pipe problem). It is bash specific.

From the bash manual:

Here Strings

   A variant of here documents, the format is:

          <<<word

   The word is expanded and supplied to the command on its standard input.

The key is the "word is expanded" bit.

like image 32
blueFast Avatar answered Oct 14 '22 01:10

blueFast


lines=`wc file.txt | awk '{print $1}'`
words=`wc file.txt | awk '{print $2}'`
...

you can also store the wc result somewhere first.. and then parse it.. if you're picky about performance :)

like image 24
duedl0r Avatar answered Oct 14 '22 00:10

duedl0r


Just to add another variant --

set -- `wc file.txt`
chars=$1
words=$2
lines=$3

This obviously clobbers $* and related variables. Unlike some of the other solutions here, it is portable to other Bourne shells.

like image 37
tripleee Avatar answered Oct 14 '22 01:10

tripleee


I wanted to store the number of csv file in a variable. The following worked for me:

CSV_COUNT=$(ls ./pathToSubdirectory | grep ".csv" | wc -l | xargs)
  • xargs removes the whitespace from the wc command
  • I ran this bash script not in the same folder as the csv files. Thus, the pathToSubdirectory
like image 20
simibac Avatar answered Oct 13 '22 23:10

simibac