Linux Bash比较目录中的字符串
嗨,我正在尝试比较目录中的两个字符串。格式如下Linux Bash比较目录中的字符串,linux,bash,unix,Linux,Bash,Unix,嗨,我正在尝试比较目录中的两个字符串。格式如下 {sametext}差异{sametext} 注意:{sametext}对于每个文件都不是静态的 比如说 myfile\u 1\u exercise.txt与myfile\u 2\u exercise.txt 您能告诉我如何在if语句中匹配上述字符串吗 基本上,我需要知道如何忽略两个字符串中的数字,使它们相同 下面显示了一些示例代码: 我的示例代码如下所示: for g in `ls -d */`; do
{sametext}差异{sametext}
注意:{sametext}对于每个文件都不是静态的
比如说
myfile\u 1\u exercise.txt
与myfile\u 2\u exercise.txt
您能告诉我如何在if语句中匹配上述字符串吗
基本上,我需要知道如何忽略两个字符串中的数字,使它们相同
下面显示了一些示例代码:
我的示例代码如下所示:
for g in `ls -d */`;
do
if [ -d $g ]; then
cd $g # down 1 directories
for h in `ls *root`;
do
printf "${Process[${count}]} = ${basedir}/${f}${g}${h}\n"
h1=${h}
if [ "${h1}" = "${h2}" ]; then # NEED to MATCH SOME HOW??????????
echo we have a match
fi
h2=${h1}
let count+=1
done
cd ../
#printf "\n\n\n\n"
fi
done
测试应该是什么来确定这一点而不是“${h1}”=“${h2}”
干杯
迈克如果你真的在比较你提到的两个文件。。。可能您可以像这样使用
diff
命令
diff myfile_1_exercise.txt myfile_2_exercise.txt
如果你真的在比较你提到的两个文件。。。可能您可以像这样使用
diff
命令
diff myfile_1_exercise.txt myfile_2_exercise.txt
免责声明:
$ bash compare.sh myfile_1_exercise.txt myfile_2_exercise.txt
match!
$ bash compare.sh myfile_1_exercise.txt otherfile_2_exercise.txt
$
剧本:
#!/bin/bash
fname1=$1
fname2=$2
findStartMatch() {
match=""
rest1=$1 ;
rest2=$2 ;
char1=""
char2=""
while [[ "$rest1" != "" && "$rest2" != "" && "$char1" == "$char2" ]] ; do
char1=$(echo $rest1 | sed 's/\(.\).*/\1/');
rest1=$(echo $rest1 | sed 's/.\(.*\)/\1/') ;
char2=$(echo $rest2 | sed 's/\(.\).*/\1/');
rest2=$(echo $rest2 | sed 's/.\(.*\)/\1/') ;
if [[ "$char1" == "$char2" ]] ; then
match="${match}${char1}"
fi
done
}
findEndMatch() {
match=""
rest1=$1 ;
rest2=$2 ;
char1=""
char2=""
while [[ "$rest1" != "" && "$rest2" != "" && "$char1" == "$char2" ]] ; do
char1=$(echo $rest1 | sed 's/.*\(.\)/\1/');
rest1=$(echo $rest1 | sed 's/\(.*\)./\1/') ;
char2=$(echo $rest2 | sed 's/.*\(.\)/\1/');
rest2=$(echo $rest2 | sed 's/\(.*\)./\1/') ;
if [[ "$char1" == "$char2" ]] ; then
match="${char1}${match}"
fi
done
}
findStartMatch $fname1 $fname2
startMatch=$match
findEndMatch $fname1 $fname2
endMatch=$match
if [[ "$startMatch" != "" && "$endMatch" != "" ]] ; then
echo "match!"
fi
免责声明:
$ bash compare.sh myfile_1_exercise.txt myfile_2_exercise.txt
match!
$ bash compare.sh myfile_1_exercise.txt otherfile_2_exercise.txt
$
剧本:
#!/bin/bash
fname1=$1
fname2=$2
findStartMatch() {
match=""
rest1=$1 ;
rest2=$2 ;
char1=""
char2=""
while [[ "$rest1" != "" && "$rest2" != "" && "$char1" == "$char2" ]] ; do
char1=$(echo $rest1 | sed 's/\(.\).*/\1/');
rest1=$(echo $rest1 | sed 's/.\(.*\)/\1/') ;
char2=$(echo $rest2 | sed 's/\(.\).*/\1/');
rest2=$(echo $rest2 | sed 's/.\(.*\)/\1/') ;
if [[ "$char1" == "$char2" ]] ; then
match="${match}${char1}"
fi
done
}
findEndMatch() {
match=""
rest1=$1 ;
rest2=$2 ;
char1=""
char2=""
while [[ "$rest1" != "" && "$rest2" != "" && "$char1" == "$char2" ]] ; do
char1=$(echo $rest1 | sed 's/.*\(.\)/\1/');
rest1=$(echo $rest1 | sed 's/\(.*\)./\1/') ;
char2=$(echo $rest2 | sed 's/.*\(.\)/\1/');
rest2=$(echo $rest2 | sed 's/\(.*\)./\1/') ;
if [[ "$char1" == "$char2" ]] ; then
match="${char1}${match}"
fi
done
}
findStartMatch $fname1 $fname2
startMatch=$match
findEndMatch $fname1 $fname2
endMatch=$match
if [[ "$startMatch" != "" && "$endMatch" != "" ]] ; then
echo "match!"
fi
sed
在这里很方便
这基本上会遍历目录中的每个文件,从文件名中提取两个字符串,并保留所有唯一组合的列表
然后,它遍历这个列表,并使用bash的通配符扩展来允许您在每个集合上循环
编辑:摆脱了一个丑陋的黑客
i=0
for f in *_*_*.txt
do
a=`echo "$f" | sed 's/\(.*\)_.*_\(.*\).txt/\1/g'`
b=`echo "$f" | sed 's/\(.*\)_.*_\(.*\).txt/\2/g'`
tmp=${all[@]}
expr match "$tmp" ".*$a:$b.*" >/dev/null
if [ "$?" == "1" ]
then
all[i]="$a:$b"
let i+=1
fi
done
for f in ${all[@]}
do
a=`echo "$f" | sed 's/\(.*\):\(.*\)/\1/g'`
b=`echo "$f" | sed 's/\(.*\):\(.*\)/\2/g'`
echo $a - $b
for f2 in $a_*_$b.txt
do
echo " $f2"
# ...
done
done
当然,这假设您关心的所有文件都遵循
*.*.*.txt
模式。sed
在这里很方便
"myfile_1_exercise.txt" == "myfile_2_exercise.txt"
这基本上会遍历目录中的每个文件,从文件名中提取两个字符串,并保留所有唯一组合的列表
然后,它遍历这个列表,并使用bash的通配符扩展来允许您在每个集合上循环
编辑:摆脱了一个丑陋的黑客
i=0
for f in *_*_*.txt
do
a=`echo "$f" | sed 's/\(.*\)_.*_\(.*\).txt/\1/g'`
b=`echo "$f" | sed 's/\(.*\)_.*_\(.*\).txt/\2/g'`
tmp=${all[@]}
expr match "$tmp" ".*$a:$b.*" >/dev/null
if [ "$?" == "1" ]
then
all[i]="$a:$b"
let i+=1
fi
done
for f in ${all[@]}
do
a=`echo "$f" | sed 's/\(.*\):\(.*\)/\1/g'`
b=`echo "$f" | sed 's/\(.*\):\(.*\)/\2/g'`
echo $a - $b
for f2 in $a_*_$b.txt
do
echo " $f2"
# ...
done
done
当然,这假设您关心的所有文件都遵循*\u*.*.txt
模式
"myfile_1_exercise.txt" == "myfile_2_exercise.txt"
你是说上面的测试应该返回true
(忽略数字)对吗?这就是我应该做的:
h1="myfile_1_exercise.txt"
h2="myfile_2_exercise.txt"
if [ $( echo ${h1} | sed 's/[0-9]*//g' ) == $( echo ${h2} | sed 's/[0-9]*//g' ) ] ; then
# do something here.
fi
你是说上面的测试应该返回true
(忽略数字)对吗?这就是我应该做的:
h1="myfile_1_exercise.txt"
h2="myfile_2_exercise.txt"
if [ $( echo ${h1} | sed 's/[0-9]*//g' ) == $( echo ${h2} | sed 's/[0-9]*//g' ) ] ; then
# do something here.
fi
当你说目录中的字符串时,你实际上指的是文件名吗?我已经读了你的帖子三次了,仍然不知道你在问什么。是
{sametext}
片段将依赖于文件,还是像myfile
这样的静态值?换句话说,您是否只查看以myfile
开头的文件?您对“${h1}”=“${h2}”的测试应该可以很好地使用test命令(也称为左方括号)比较字符串。当然,h2第一次是空的。Sorr namuol,{sametext}不是静态的。当你说目录中的字符串时,你实际上是指文件名吗?我已经读了你的帖子3次了,仍然不知道,你在问什么。{sametext}
这篇文章是否取决于文件,或者它是某种静态值,比如myfile
?换句话说,您是否只查看以myfile
开头的文件?您对“${h1}”=“${h2}”的测试应该可以很好地使用test命令(也称为左方括号)比较字符串。当然h2第一次是空的,{sametext}不是静态的。不是文件内容。对不起,我应该说得更清楚。我在比较文件名。不是文件内容。对不起,我应该说得更清楚些