我有幾個.csv文件,如下所示。如何在shell腳本中異步運行迭代
- [email protected]#-1637746436.csv
- [email protected]#-1637746436.csv
- [email protected]#-1637746439.csv
- [email protected]#-1637746436.csv
- [email protected]#-1637746439.csv
我寫了一個腳本任務,下面來執行:
- 根據我們作爲參數傳遞給腳本的模式獲取大文件。
- 合併所有其他具有相同模式的文件並創建一個新文件
- 從新文件中刪除重複標題。
- 根據作爲參數傳遞的參數將新文件移動到目標位置。
例子:我傳遞 「1637746436 @家/ dest1,1637746436 @家/ dest2」 作爲 第二個參數的腳本。下面的腳本將獲取 模式(1637746436)。獲取更大的文件併合並所有其他 文件(具有相同的模式)。新文件將被創建,同樣將被移動到目的地(home/dest1)。
以下腳本將按順序執行模式匹配和執行。
如何讓'循環迭代'應該並行執行?我的意思是應該同時執行「1637746436 @ home/dest1,1637746436 @ home/dest2」的模式匹配(不是一個接一個)。
請幫忙。
$merge.sh /home/dummy/17 "[email protected]/dest1,[email protected]/dest2"
#!/bin/bash
current=`pwd`
source=$1
destination=$2
echo "$destination" | tr "," "\n" > $current/out.txt
cat out.txt | cut -d "@" -f1 > $current/pattern.txt
for var in `cat pattern.txt`
do
getBiggerfile=$(ls -Sl $source/*$var.csv | head -1)
cd $source
getFileName=$(echo $getBiggerfile | cut -d " " -f9-)
newFileName=$(echo $getFileName | cut -d "@" -f1)
cat *$var.csv >> $getFileName
header=$(head -n 1 $getFileName)
(printf "%s\n" "$header";
grep -vFxe "$header" $getFileName
) > $newFileName.csv
rm -rf *$var.csv
cd $current
for var1 in `cat out.txt`
do
target=`echo $var1 | cut -d "@" -f2`
id=$(echo $var1 | cut -c-10)
if [ $id = $var ]
then
mv $newFileName.csv $target
fi
done
done
潛在的跨站點重複:https://unix.stackexchange.com/questions/103920/parallelize-a-bash-for-loop – Aserre