我需要將幾個檔案中的數字提取到一個累積變數中,并為每個父目錄列印該變數,如圖所示
。├──Parent1
│ ├───20210824_2000
│ │ ├──200000_child1
│ │ report.md
│ │ log.log
│ │ file.xml
│ │ input.json
│ │ │
│ │ ├──200030_child2
│ │ │
│ │ ├──200034_child3
│ │ │
│ │ ├──200039_child4
│ │ ...
│ ├───20210825_0800
│ │ ├──200000_child1
│ │ report.md
│ │ log.log
│ │ file.xml
│ │ input.json
│ │ │
│ │ ├──200030_child2
│ │ │
│ │ ├──200034_child3
│ │ │
│ │ ├──200039_child4
│ │ ...
│ ...
├──父母2
│ ├───20210824_2000
│ │ ├──200000_child1
│ │ report.md
│ │ log.log
│ │ file.xml
│ │ input.json
│ │ │
│ │ ├──200030_child2
│ │ │
│ │ ├──200034_child3
│ │ │
│ │ ├──200039_child4
│ │ ...
│ ├───20210825_0800
│ │ ├──200000_child1
│ │ report.md
│ │ log.log
│ │ file.xml
│ │ input.json
│ │ │
│ │ ├──200030_child2
│ │ │
│ │ ├──200034_child3
│ │ │
│ │ ├──200039_child4
│ │ ...
│ ...
...
。
我似乎無法將grep輸出提取到一個數字變數中。子檔案夾有一個附加的時間戳,所以我進行排序,因為我只想要最新的檔案。
這是迄今為止我所擁有的:
#!/bin/bash
找到 ... -type d -iname 'parent*' | while read -r dir; do
sum=0。
find "$dir" -maxdepth 1 -type d| sort -r | head -1 |
(
while read -r subdir; do
count=$(find "$subdir" -type f -iname '*report.md' -exec grep -ohP '(?<=*)/span>d (?=* number of things) ' {} )".
sum=$((sum count))
done。
basename "$dir" "$sum"
)
done。
count和sum,而只是將每個檔案的count列印到控制臺。
uj5u.com熱心網友回復:
有一個問題是由于每個while創建的子shell,所以變數不是全域的,你可以在"在while回圈內設定的shell變數在其外不可見"中找到有用的資訊
。另外,count變數可能有問題,因為find應該回傳多行。
用這個試試:
找到 ... -type d -iname 'parent*' | while read -r dir; do
sum=0。
while read -r subdir; do
while read report; do
count="$(grep -ohP '(?<=*)d (?=* 事數)' $report) "
sum=$((sum count))
done < <(find "$subdir" -type f -iname'*report.md')
done < <(find "$dir" -maxdepth 1 -type d | sort -r | head -1)
folder=$(basename $dir)
echo "$folder $sum"/span>。
done。
請注意done < <(.
我沒有測驗,抱歉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/308008.html
標籤:
上一篇:在其他Unix命令中使用DatabricksCLI命令輸出
下一篇:jq提取json的資料
