遇到一个笔试题,直接放弃了,看来我对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
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”的行打印出来。