Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File Shell脚本:具有相同字母的文件名_File_Shell_Sorting_Alphabet - Fatal编程技术网

File Shell脚本:具有相同字母的文件名

File Shell脚本:具有相同字母的文件名,file,shell,sorting,alphabet,File,Shell,Sorting,Alphabet,任务是列出一个目录中包含相同字母的所有文件名,唯一的区别是字母的顺序 如asd.txt和dsa.txt powershell中有一个工作代码: for i in `ls -v $dir`; do temp=$(grep -o . <<<"$i"|sort|tr -d "\n") temper=$i for j in `ls -v $dir`; do temp2=$(grep -o . <<<"$j"|sort|tr -d "\n")

任务是列出一个目录中包含相同字母的所有文件名,唯一的区别是字母的顺序 如asd.txtdsa.txt

powershell中有一个工作代码:

 for i in `ls -v $dir`; 
do
 temp=$(grep -o . <<<"$i"|sort|tr -d "\n")
 temper=$i
 for j in `ls -v $dir`; 
 do
    temp2=$(grep -o . <<<"$j"|sort|tr -d "\n")  
    if [ "$temp" = "$temp2" ] && [ "$temper" != "$j" ];
    then
        echo $temper
        echo $j
    fi
 done;
done;
用于'ls-v$dir'中的i;
做
温度=$(grep-o.bash

对于名称中带有空格的文件,这将中断:

getchars () { echo "$1" | sed 's/./&\n/g' | sort | tr -d '\n'; }
declare -A files
for file in *; do files["$(getchars $file)"]+="$file "; done
在包含文件的目录中测试
asdf.txt
fdsa.txt
foo
foobar
tadxfst.

for key in "${!files[@]}"; do printf "%s\t%s\n" "$key" "${files[$key]}"; done
根据您对这些组所做的操作,我将使用类似perl的内容:

perl -e '
    opendir $dir, ".";
    while (readdir $dir) {
        next if /^\.\.?$/;
        push @{$files{join "", sort split //}}, $_;
    } 
    # now do something with the files
    use Data::Dumper;
    print Dumper \%files
'
perl -e '
    opendir $dir, ".";
    while (readdir $dir) {
        next if /^\.\.?$/;
        push @{$files{join "", sort split //}}, $_;
    } 
    # now do something with the files
    use Data::Dumper;
    print Dumper \%files
'
$VAR1 = {
          '.adfsttx' => [
                          'asdf.txt',
                          'fdsa.txt',
                          'tadxfst.'
                        ],
          'foo' => [
                     'foo'
                   ],
          'abfoor' => [
                        'foobar'
                      ]
        };