遇到一個筆試題,直接放棄了,看來我對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”的行打印出來。