繁体   English   中英

Bash 比较目录中的字符串

[英]Bash Comparing strings in a directory

您好我正在尝试比较目录中的两个字符串。 格式如下。

{sametext}difference{sametext}

注意:{sametext} 不是每个文件的 static

例如

myfile_1_exercise.txtmyfile_2_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}"

干杯,

麦克风

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模式。

"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

免责声明:

  1. 里程可能会有所不同,您可能必须针对极端情况调整和调试脚本。
  2. 您最好使用 Perl 来完成您的任务。
  3. 即使在 Bash 中也可能有更好的解决方案。 这个效率不是很高,但似乎有效。

也就是说,这是一个根据您的要求比较两个字符串的脚本。 我相信你可以弄清楚如何在你的目录列表脚本中使用它(你可能想顺便考虑一下find

该脚本需要两个字符串并打印匹配! 如果他们匹配

$ 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

如果您实际上是在比较您提到的两个文件...也许您可以使用diff命令,例如

diff myfile_1_exercise.txt myfile_2_exercise.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM