开发者

results of wc as variables

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 assoc开发者_运维知识库iated to the values 2, 5, and 23. How can I do that in bash?


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)


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.


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 :)


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.


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


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


declare -a result
result=( $(wc < file.txt) )
lines=${result[0]}
words=${result[1]}
characters=${result[2]}
echo "Lines: $lines, Words: $words, Characters: $characters"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜