我想使用来自'wc'的行作为变量。例如:
echo 'foo bar' > file.txt
echo 'blah blah blah' >> file.txt
wc file.txt
2 5 23 file.txt
我想要像$lines
, $words
, 和 $characters
这样的变量与值2
, 5
, 和 23
关联起来。在bash中我该怎么做?
使用纯Bash:(没有awk)
a=($(wc file.txt))
lines=${a[0]}
words=${a[1]}
chars=${a[2]}
这个方法使用了bash的数组. a=(1 2 3)
创建了一个包含元素1、2和3的数组。然后我们可以使用语法${a[indice]}
来访问不同的元素。
替代方案:(基于gonvaled解决方案)
read lines words chars <<< $(wc x)
或者在sh中:
a=$(wc file.txt)
lines=$(echo $a|cut -d' ' -f1)
words=$(echo $a|cut -d' ' -f2)
chars=$(echo $a|cut -d' ' -f3)
还有其他解决方案,但我通常使用的一个简单方法是将wc
的输出放入临时文件中,然后从那里读取:
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
这种方法的优点是您不需要为每个变量创建多个awk
进程。缺点是您需要一个临时文件,之后您应该删除它。
请注意:这样做是无效的:
wc file.txt | read lines words characters filename
问题在于管道输入到read
时会创建另一个进程,在那里更新的变量无法在调用shell中访问。
编辑:添加arnaud576875的解决方案:
read lines words chars filename <<< $(wc x)
无需写入文件(也不存在管道问题)即可运行。它是特定于bash的。
来自Bash手册:
Here Strings
A variant of here documents, the format is:
<<<word
The word is expanded and supplied to the command on its standard input.
关键是“单词已展开”这一点。
xxx
或文件名:wc < file.txt | read lines words characters
。 - Adrian Pronkread lines words chars <<< $(wc x)
可以在不写入文件的情况下工作(并且不会出现管道问题)。 - Arnaud Le Blanc<<<
! - Adrian Pronklines=`wc file.txt | awk '{print $1}'`
words=`wc file.txt | awk '{print $2}'`
...
如果您在意性能,您也可以先将wc
结果存储在某个地方,然后再解析它。
set -- `wc file.txt`
chars=$1
words=$2
lines=$3
$*
和相关变量。与其他一些解决方案不同,这种方法可以在其他Bourne shell中使用。我想将csv文件的数量存储在一个变量中。以下对我有用:
CSV_COUNT=$(ls ./pathToSubdirectory | grep ".csv" | wc -l | xargs)
你可以通过打开子 shell 将输出分配给变量:
$ x=$(wc some-file)
$ echo $x
1 6 60 some-file
现在,为了获取单独的变量,最简单的选择是使用 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"
(...)
创建一个数组,后面的行对其进行索引。否则,结果只是三个数字的单个字符串。 - Adrian Pronka=x y z
将x
分配给a
,然后运行命令y z
;a=(x y z)
将x
、y
和z
分配给数组a
,但是您不必像a=($(echo x y z))
那样输入额外的括号,因为单词拆分稍后发生。 - Thom Boyer