![](/img/trans.png)
[英]How can i add a timestamp to my script using the linux free command (free | grep mem | awk '{print $4/$2 * 100.0}')
[英]Using grep command, I can print the data that match with my command but how to print an error when data is not available?
我在子目錄中有device.json文件,並且從該.json文件中獲取文件,我正在嘗試打印data = xxx值,並且正在使用以下Grep命令,但是當device.json文件在以下位置不可用時,我也想打印錯誤子目錄之一。
找 。 名稱“ device.json” | xargs grep -E'數據'
如果要了解不包含“ device.json”條目的目錄,請find . -name device.json
find . -name device.json
不會刪除它。 您必須執行以下操作:
find . -type d -exec sh -c 'for x; do f="$x/device.json"; if test -f "$f";
then grep -H data "$f"; else echo "$f: No such file" >&2; fi; done' _ {} \+
嘗試類似:
find . -type d |
while IFS= read -r dir; do
file="$dir/device.json"
if [ -f "$file" ]; then
grep 'data' "$file"
else
printf '"%s" does not exist\n' "$file"
fi
done
僅當您的目錄名稱可以包含換行符時,它才會失敗。 顯然, grep 'data'
不是在json文件中找到所需值的可靠方法,但這是一個單獨的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.