代码之家  ›  专栏  ›  技术社区  ›  Blake Smreker

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

  •  2
  • Blake Smreker  · 技术社区  · 6 年前

    因此,在我的脚本开始时,我定义了“阈值”,它有一个在下一部分中不存在的数字,(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"
    

    的输出 磁盘空间 在我的测试服务器上是:

    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
    

    如上所述,我的变量中的“2”不存在,而“6”存在。我的目标是让grep找到与变量中的数字匹配的任何数字。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Allan    6 年前

    让我们考虑一下 cat output_df 是您的 df -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输出\u df 通过 磁盘空间 在最后的脚本中。

    这将给出输出:

    /dev
    /
    /dev/shm
    /run/lock
    

    说明:

    • thresold="2%|6%" 是匹配的正则表达式 2% 6% 你可以把它推广到 "2%|6%|X%|Y%|...|Z%"
    • 通过将其作为变量传递给awk -v VAR=$thresold
    • 然后你命令 awk 当第5个字段与您通过传递给它的正则表达式匹配时,打印第6个字段 '{if($5~VAR)print $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 (静音 标准错误 )

        2
  •  1
  •   David C. Rankin    6 年前

    初始化 threshold 作为 索引数组 ,则,

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

    然后你打电话 grep 使用:

    grep $threshold dffile
    

    自从 门槛 是数组,若要取消对数组中所有值的引用,请使用以下格式:

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

    当您将数组视为普通变量时,例如。 $threshold 您只返回第一个元素,例如。

    echo $threshold  ##  output '2%'
    

    因此,在继续之前,您需要确定要传递到的内容 格雷普 ,如果要搜索 2% 6% ,那么艾伦在上面有一个很好的解释。您还可以构造 格雷普 表达式使用 printf -v ,例如。

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

    或适当限制前2个元素,

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

    然后打电话 格雷普 具有

    grep "$gexp" dffile >> thresh
    

    如果您还有其他问题,请告诉我。