笔试题:递归打印文件夹内所有文件中包含xxx的行

来源:互联网 发布:win7网络里的网络设施 编辑:程序博客网 时间:2024/06/03 22:47
遇到一个笔试题,直接放弃了,看来我对shell是真不熟练呀……回来后,通过查询,实现了一个版本。
#!/bin/bash

function dealxml()
{
    for file in $1/*
        do  
            if [ -d "$file" ];
            then
                dealxml $file
            else
                echoxml $file
            fi  
        done
}

function echoxml()
{
    while read line
    do
        if [[ "$line" =~ xml$ ]]  #if you want to find the line with the end of 'xml', the regex pattern is xml$
            then
            echo $line
        fi  
    done < $1
}

if [ -d "$1" ];
then
    dealxml $1
else
    dealxml .
fi
第一个方法是递归遍历文件夹
第二个方法是处理文件中的内容,把包含“XXX”的行打印出来。
 


来自为知笔记(Wiz)


0 0
原创粉丝点击