如何获取grep来搜索我在变量中输入的任何数字?

时间:2018-06-01 00:57:34

标签: linux bash shell unix server-administration

所以在我的脚本开头,我正在定义"阈值,"它具有一个不会存在于下一部分中的数字,(2)和一个将存在于下一部分(6)中的数字。我将df -h运行到名为dffile的文件中。我的问题是,如何在第7行获取grep来搜索所有变量" threshold"对于文件中存在的数字?如果我在变量中有2之前的6,它就可以工作,所以看起来好像它只搜索其中的第一个数字。谢谢!

#!/bin/bash

threshold=("2%" "6%")

df -h > dffile

grep $threshold dffile >> thresh

cat thresh | awk '{print $6}' >> finding1

LINES=()
while IFS= read -r finding1
do
find $finding1 -xdev -size +40M -exec ls -lah {} \; | head -n 10
done < "finding1"

我的测试服务器上df -h的输出是:

root@tstd0001:~/scripts# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            481M     0  481M   0% /dev
tmpfs            99M  616K   98M   1% /run
/dev/vda1        25G  1.3G   23G   6% /
tmpfs           493M     0  493M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           493M     0  493M   0% /sys/fs/cgroup
/dev/vda15      105M  3.4M  102M   4% /boot/efi
tmpfs            99M     0   99M   0% /run/user/0

如上所示,&#34; 2&#34;从我的变量,不存在,而&#34; 6&#34;确实。我的目标是让grep找到与变量内部的数字匹配的任何数字。

2 个答案:

答案 0 :(得分:1)

让我们考虑cat output_dfdf -h命令的输出:

$ cat output_df
Filesystem      Size  Used Avail Use% Mounted on
udev            481M     0  481M   2% /dev
tmpfs            99M  616K   98M   1% /run
/dev/vda1        25G  1.3G   23G   6% /
tmpfs           493M     0  493M   6% /dev/shm
tmpfs           5.0M     0  5.0M   2% /run/lock
tmpfs           493M     0  493M   0% /sys/fs/cgroup
/dev/vda15      105M  3.4M  102M   4% /boot/efi
tmpfs            99M     0   99M   0% /run/user/0

然后以下列方式更改脚本的第一部分:

thresold="2%|6%"
cat output_df | awk -v VAR=$thresold '{if($5~VAR)print $6}'

当然,您必须在最终脚本中将cat output_df替换为df -h

这将给出输出:

/dev
/
/dev/shm
/run/lock

说明:

  • thresold="2%|6%"是匹配2%6%的正则表达式,您可以将其概括为"2%|6%|X%|Y%|...|Z%"
  • 您通过-v VAR=$thresold
  • 将其作为变量传递给awk
  • 然后,当第5个字段与您通过awk传递给它的正则表达式匹配时,您命令'{if($5~VAR)print $6}'打印第6个字段

然后您可以重新组合所有内容而不使用中间文件:

thresold="2%|6%"
for f in `df -h | awk -v VAR=$thresold '{if($5~VAR)print $6}'`
do 
   find $f -xdev -size +40M -exec ls -lah {} \; 2>/dev/null | head -n10
done

在我的方框中,它提供以下输出:

-rw-r--r-- 1 root root 47M  6月  1 06:33 /var/cache/apt/srcpkgcache.bin
-rw-r--r-- 1 root root 47M  6月  1 06:33 /var/cache/apt/pkgcache.bin
-rw-r--r-- 1 root root 62M  4月 28 02:27 /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar
-rw-r--r-- 1 root root 56M  2月 20 06:10 /usr/lib/libreoffice/program/libmergedlo.so
-rw-r--r-- 1 root root 73M  5月 22 19:31 /usr/lib/thunderbird/libxul.so
-rwxr-xr-x 1 root root 142M  5月 22 01:35 /usr/lib/chromium-browser/chromium-browser
-rw-r--r-- 1 root root 41M  5月  8 09:57 /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37.28.2
-rw-r--r-- 1 root root 54M 11月 17  2017 /usr/lib/x86_64-linux-gnu/libLLVM-5.0.so.1
-rw-r--r-- 1 root root 99M  3月 18  2017 /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
-rwxr-xr-x 1 root root 42M  5月  8 09:57 /usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/WebKitPluginProcess2
 ...

注意:2>/dev/null此重定向是通过将stderr重定向到/dev/null(将stderr静音)来删除所有权限错误

答案 1 :(得分:1)

您将threshold初始化为索引数组

threshold=("2%" "6%")

然后使用以下代码致电grep

grep $threshold dffile

由于threshold是一个数组,要取消引用数组中的所有值,请使用以下格式:

${threshold[@]}   ## which can be quoted to preserve whitespace in elements

当您将数组作为正常变量时,例如, $threshold只返回第一个元素,例如

echo $threshold  ##  output '2%'

因此,在进一步研究之前,您需要确定要传递给grep的内容,如果您要搜索2%6%,那么Allan上面有一个很好的解释。您还可以使用grep构建printf -v表达式,例如

printf -v gexp "%s\|%s" ${threshold[@]}

或适当限制前两个元素,

printf -v gexp "%s\|%s" "${threshold[0]}" "${threshold[1]}"

然后使用

调用grep
grep "$gexp" dffile >> thresh

如果您还有其他问题,请与我们联系。