首页 > 技术文章 > shell命令从目录中循环匹配关键词

gide 2016-06-18 13:31 原文

#!/bin/bash

while read line
do
   for file in /home/local/test/*
   do
        if test -f $file
        then
            sed -n "/${line}/p" $file >> /home/local/out.txt
        fi
   done
done < macid.txt

 任务:要从home/local/test 文件夹下面的文件

  file_1.log

  file_2.log

  ....

  文件中把包含macid.txt文件的 关键词查找出来,并输出重定向到out.txt文件中

需要两个嵌套循环实现

循环macid.txt中的关键词和循环被查找目录的文件,具体代码如上。

另外如果需要把linux上面的文件同步到windows上可以用pscp.exe这个工具

可以到putty 下载

地址:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

下载后放到d盘下,然后cmd 进入d盘操作

D:\>pscp.exe root@/home/local/test/test.txt d:/

如果是目录则需要加-r  pscp.exe -r

推荐阅读