36

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?

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
719016
  • 9,922
  • 20
  • 85
  • 158

7 Answers7

47

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)
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • Can you explain why this works? In particular, what do the extra parentheses around the first line do? Without them, it no longer works so they appear to be pretty significant. – Konrad Rudolph Aug 19 '11 at 10:05
  • In bash, the outer `(...)` create an array which the later lines index. Otherwise the result is just aa single string of three numbers – Adrian Pronk Aug 19 '11 at 10:12
  • In this case, the parentheses are part of the $(_command_) command substitution syntax; it is equivalent to \`command\`, but the parenthesis notation simplifies nesting. The _command_ is run in a subshell, and the text of the command substitution is replaced by the output of _command_, almost as if you had typed it. – Thom Boyer Aug 23 '17 at 12:54
  • The "almost as if" is because `a=x y z` assigns `x` to `a` and then runs the command `y z`; `a=(x y z)` assigns `x`, `y`, and `z` to the array `a`, but you don't have to type the extra parens as in `a=($(echo x y z))` because the word splitting happens later. – Thom Boyer Aug 23 '17 at 13:04
18

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.

blueFast
  • 41,341
  • 63
  • 198
  • 344
4

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
simibac
  • 7,672
  • 3
  • 36
  • 48
4
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 :)

duedl0r
  • 9,289
  • 3
  • 30
  • 45
4

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

You can assign output to a variable by opening a sub shell:

$ x=$(wc some-file)
$ echo $x
1 6 60 some-file

Now, in order to get the separate variables, the simplest option is to use awk:

$ x=$(wc some-file | awk '{print $1}')
$ echo $x
1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0
declare -a result
result=( $(wc < file.txt) )
lines=${result[0]}
words=${result[1]}
characters=${result[2]}
echo "Lines: $lines, Words: $words, Characters: $characters"
Adrian Pronk
  • 13,486
  • 7
  • 36
  • 60