【问题标题】:Bash & Perl script to convert relative paths to absolute paths将相对路径转换为绝对路径的 Bash 和 Perl 脚本
【发布时间】:2018-02-25 16:48:06
【问题描述】:

我有一个顶级目录路径,我想递归地将所有相对路径转换为该目录内所有文件中存在的绝对路径。 例如我有这个目录结构:

$ tree
.
|-- DIR
|   |-- inner_level.ext1
|   `-- inner_level.ext2
|-- top_level.ext1
`-- top_level.ext2

top_level.ext1的内容:

../../a/b/c/filename_1.txt
../../a/d/e/filename_2.txt

假设顶层目录路径是/this/is/the/abs/dir/path/

想将top_level.ext1的内容转换为:

/this/is/the/abs/a/b/c/filename_1.txt
/this/is/the/abs/a/d/e/filename_2.txt

top_level.ext2的内容:

cc_include+=-I../../util1/src/module1/moduleController -I../../util/src/module1/module2Controller;
cc_include+=-I../../util2/src/module2/moduleUtility;

想将top_level.ext2的内容转换为:

cc_include+=-I/this/is/the/abs/util1/src/module1/moduleController -I/this/is/the/abs/util/src/module1/module2Controller;
cc_include+=-I/this/is/the/abs/util2/src/module2/moduleUtility;

另外,想对 DIR 中的文件应用同样的转换。

例如 DIR/inner_level.ext1的内容:

../../../a/b/c/filename_1.txt
../../../a/d/e/filename_2.txt

想将DIR/inner_level.ext1的内容转换为:

/this/is/the/abs/a/b/c/filename_1.txt
/this/is/the/abs/a/d/e/filename_2.txt

DIR/inner_level.ext2 也一样。

写了这两个脚本。

top_level.ext1 转换成功。

file_manager.sh:

#!/usr/bin/bash
file='resolve_path.pl'
basedir='/this/is/the/abs/dir/path'

run_perl(){
    echo -e "\n File getting modified: $1"
    cp $1 tmp.in
    perl $file 
    mv tmp.out $1
    rm tmp.in
}

find $basedir -type f |while read inputfile
do 
    run_perl $inputfile
done

resolve_path.pl:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use 5.010;
use Switch;

#******************************************************
#       Set-up Directory And Input/Output File Names
#******************************************************
our $in_file  = glob('tmp.in');
my $out_file1 = 'tmp.out';
print "Input file: $in_file\n";

#************************************
#       Local and Global Variables
#*************************************
my $current_path = "/this/is/the/abs/dir/path";
my $temp_path = $current_path;

#************************************
#       Open Read and Write File
#************************************
open(READ, $in_file) || die "cannot open $in_file";
open(WRITE, ">$out_file1") || die "cannot open $out_file1";


#******************************************************
#       Read The Input [*.out] File Line By Line
#******************************************************
while (<READ>) {
    if(/^(\.\.\/){1,}(\w+\/)*(\w+).(\w+)/){
      my $file_name = $3;
        my $file_ext  = $4;

        my @count = ($_ =~ /\.\.\//g);
        my $cnt = @count;

        my @prev_dir = ($_ =~ /\w+\//g);
        my $prev_dir_cnt = @prev_dir;
        my $file_prev_dir = join('', @prev_dir);

        $temp_path = $current_path;
        for(my $i=0; $i<$cnt; $i++){
            if($temp_path =~m/(\/.*)\/\w+/){
                $temp_path = $1;
            }
        }

        print WRITE "$temp_path"."\/"."$file_prev_dir"."$file_name"."\."."$file_ext"."\n";

    } else {
        print WRITE "$_";
    }
}

我面临的问题:

  1. 不会对 top_level.ext2DIR/inner_level.ext2 应用任何转换 因为我的 Perl 脚本没有正确解析 ../es(即 cc_include+=-I 刚开始)。

  2. 从相对路径到绝对路径的转换不起作用 适合DIR/inner_level.ext1 并且路径错误 附加。

如果有人可以建议对我的脚本进行预期更改以解决上述两个问题,那将很有帮助。

【问题讨论】:

    标签: regex bash perl absolute-path


    【解决方案1】:

    为什么是 2 个脚本?那是低效的。

    Perl 完全能够检索文件列表,并且具有简化该过程的模块以及解析和更改路径的模块。

    File::Find - Traverse a directory tree.

    File::Find::Rule - Alternative interface to File::Find

    File::Basename - Parse file paths into directory, filename and suffix.

    File::Spec - portably perform operations on file names

    【讨论】:

      猜你喜欢
      • 2017-08-01
      • 2011-11-07
      • 2011-05-02
      • 2016-01-09
      相关资源
      最近更新 更多