Sqlite: 문자열 modelID1/modelID2/modelID3을 name1/name2/name3/으로 바꿉니다.

Sqlite: 문자열 modelID1/modelID2/modelID3을 name1/name2/name3/으로 바꿉니다.

내 목표는 macOS Photos.app 데이터베이스를 읽고 여기에 포함된 사진의 경로를 찾는 것입니다. 앨범과 해당 상위 폴더의 이름은 다음과 같습니다.RK 폴더각 사진마다 양식을 결정할 수 있습니다. 상위 폴더는 다른 폴더에 있을 수 있습니다. 생성된 폴더 경로를 읽을 수도 있지만 형식은 다음과 같습니다.

modelId 1/modelID 2/modelId 3/modelID 4

문자열을 실제 폴더 이름으로 대체할 충분한 지식이 없기 때문에 여기서 더 이상 진행할 수 없습니다. 가능한 해결책을 찾으려면 데이터베이스 복사본을 사용하고 있으므로 폴더 경로를 RKFolder 테이블에 직접 쓸 수도 있습니다.

SELECT  
modelId, name,folderPath, uuid 
FROM RKFolder 
WHERE implicitAlbumUuid not NULL

결과(발췌)

modelId name            folderPath      uuid

1                       1/              LibraryFolder
2       TopLevelAlbums  1/2/            TopLevelAlbums
7       Test            1/2/7/          kbY7RDHjRLS
8       xxx             1/2/8/          bT5WAkPWQ1
9       Test            1/2/8/9/        9PYeLZDRTne
10      ab              1/2/10/         7Cse21+1SIag
11      abc             1/2/7/11/       pNMvzDdyS%
16      efg             1/2/7/11/16/    a6R97tAxSBW

다음과 같이 교체하세요.

modelId name            folderPath                     uuid

1                       /                              LibraryFolder
2       TopLevelAlbums  /TopLevelAlbums/               TopLevelAlbums
7       Test            /TopLevelAlbums/Test/          kbY7RDHjRLS
8       xxx             /TopLevelAlbums/xxx/           bT5WAkPWQ1
9       Test            /TopLevelAlbums/xxx/Test/      9PYeLZDRTne
10      ab              /TopLevelAlbums/ab/            7Cse21+1SIag
11      abc             /TopLevelAlbums/Test/abc/      pNMvzDdyS%
16      efg             /TopLevelAlbums/Test/abc/efg/  a6R97tAxSBW

또는 TopLevelAlbums-main 폴더가 없으면 더 좋습니다.

modelId name            folderPath      uuid

16      efg             /Test/abc/efg/  a6R97tAxSBW

지금까지 사용하고 있는 스크립트는 다음과 같습니다(축약).

SELECT 
RKAlbumVersion.versionId, 
RKVersion.filename, 

(SELECT RKFolder.folderpath from RKFolder, RKAlbum 
WHERE RKFolder.uuid = RKAlbum.folderUuid 
and RKAlbum.modelID = RKAlbumVersion.albumId)

FROM RKMaster, RKAlbumVersion, RKVersion 
WHERE RKVersion.modelId = RKAlbumVersion.versionId 
and RKVersion.masterUuid = RKMaster.uuid
-->
Output:
77  001.JPG  1/2/7/11/16/

replace with:
77  001.JPG  /Test/abc/efg/

답변1

Perl을 사용하여 이 작업을 수행할 수 있습니다.

#!/usr/bin/perl

# number of parent directories to drop
$drop = 2;

open( $input, '<', $ARGV[0] );

# drop header lines
$line = <$input>;
$line = <$input>;

# third line is first data, being root has no name, was requested that it wasn't used, which is good, because it makes life simplier, assuming the first dir is always 1
$line = <$input>;
#@databits = split(/\s+/, $line);
$hash{'1'} = '';

while ( $line = <$input> ) {
    @databits = split(/\s+/, $line);
    $hash["$databits[0]"] = $databits[1];
}

close( $input );

open( $input, '<', $ARGV[0] );
# now we print!
# headers
$line = <$input>;
print "$line";
$line = <$input>;
print "$line";

# drop first data line
$line = <$input>;

while ( $line = <$input> ) {
    @databits = split(/\s+/, $line);
    @replace = split(/\//, $databits[2]);

    $count = 0; # start at the start
    foreach (@replace) {
        $replace[$count] = $hash[$_];
        $count++;
    }

    for (my $i=0; $i < $drop; $i++) {
       shift(@replace);
    }

    $replaced = join('/', @replace);
    if ( $replaced ne '' ) {
        print "$databits[0] $databits[1] /$replaced/ $databits[3]\n";
    }
}

아래 예에서는 "input"이라는 이름의 텍스트 파일로 출력을 입력합니다. 컬럼 명령어를 사용하면 아름다운 컬럼을 만들 수 있습니다.

$ ./replace.pl input | column -t
modelId  name  folderPath      uuid
7        Test  /Test/          kbY7RDHjRLS
8        xxx   /xxx/           bT5WAkPWQ1
9        Test  /xxx/Test/      9PYeLZDRTne
10       ab    /ab/            7Cse21+1SIag
11       abc   /Test/abc/      pNMvzDdyS%
16       efg   /Test/abc/efg/  a6R97tAxSBW

관련 정보