例如,假设我想数10个大文件的行数,然后打印一个总计。
for f in files
do
#this creates a background process for each file
wc -l $f | awk '{print $1}' &
done我试过这样的方法:
for f in files
do
#this does not work :/
n=$( expr $(wc -l $f | awk '{print $1}') + $n ) &
done
echo $n发布于 2013-08-09 05:20:51
您可能应该使用gnu并行:
find . -maxdepth 1 -type f | parallel --gnu 'wc -l' | awk 'BEGIN {n=0} {n += $1} END {print n}'否则,xargs采用并行模式:
find . -maxdepth 1 -type f | xargs -n1 -P4 wc -l | awk 'BEGIN {n=0} {n += $1} END {print n}'另一个选项,如果这不符合您的需要,是写到临时文件。如果不想写入磁盘,只需写入/dev/shm即可。这是大多数Linux系统上的一个ramdisk。
#!/bin/bash
declare -a temp_files
count=0
for f in *
do
if [[ -f "$f" ]]; then
temp_files[$count]="$(mktemp /dev/shm/${f}-XXXXXX)"
((count++))
fi
done
count=0
for f in *
do
if [[ -f "$f" ]]; then
cat "$f" | wc -l > "${temp_files[$count]}" &
((count++))
fi
done
wait
cat "${temp_files[@]}" | awk 'BEGIN {n=0} {n += $1} END {print n}'
for tf in "${temp_files[@]}"
do
rm "$tf"
done顺便说一句,这可以被看作是一个映射-减少与wc做映射和awk做缩减。
发布于 2013-08-09 00:04:43
我终于找到了一个使用匿名管道和bash的工作解决方案:
#!/bin/bash
# this executes a separate shell and opens a new pipe, where the
# reading endpoint is fd 3 in our shell and the writing endpoint
# stdout of the other process. Note that you don't need the
# background operator (&) as exec starts a completely independent process.
exec 3< <(./a.sh 2&1)
# ... do other stuff
# write the contents of the pipe to a variable. If the other process
# hasn't already terminated, cat will block.
output=$(cat <&3)发布于 2013-08-09 00:13:30
您可以将其写入文件或更好的文件中,在数据到达时立即收听fifo。
下面是一个关于它们如何工作的小例子:
# create the fifo
mkfifo test
# listen to it
while true; do if read line <test; then echo $line; fi done
# in another shell
echo 'hi there'
# notice 'hi there' being printed in the first shell所以你可以
for f in files
do
#this creates a background process for each file
wc -l $f | awk '{print $1}' > fifo &
done听fifo的大小。
https://stackoverflow.com/questions/18138195
复制相似问题