0

特定の座標に関するデータを含む多くのテーブルで構成されるファイルがあります。各テーブルは、「Coords」という単語を含む行で区切られています。

Coords
Table 1
Coords 
Table 2
Coords
Table 3
...

別のファイルに、テーブルに一致するすべての座標のリストがあります。

Coordinate 1
Coordinate 2
Coordinate 3
...

私がやろうとしているのは、「Coords」の最初のインスタンスを座標ファイルの最初の行に、2 番目のインスタンスを 2 行目などに置き換えることです。

Coordinate 1
Table 1
Coordinate 2
Table 2
Coordinate 3
Table 3
...

私はこれを試しました:

while read coord
do
    perl -pln -e 's/Coords/$coord/' Tables >> Output
done <Coordinates

しかし、うまくいきませんでした。(perl は bash 変数を使用できないためですか?) どんな助けでも大歓迎です。

4

3 に答える 3

1

これはうまくいくかもしれません(GNU sed):

sed -e '/Coords/{Rcoord.txt' -e 'd}' template.txt
于 2013-06-25T20:42:31.313 に答える
0

これは非常に簡単に実行できます。管理しやすいステップに分割するだけです。

私がやろうとしているのは、「Coords」の最初のインスタンスを座標ファイルの最初の行に、2 番目のインスタンスを 2 行目などに置き換えることです。

これを分割できるかどうか見てみましょう。

  1. 座標ファイルからデータを読み取ります (おそらくリストに)
  2. プレースホルダー ファイルを 1 行ずつ検索してループします。Coords
  3. 一致が見つかった場合は、その行を座標ファイルの次の行で上書きします (を使用shiftすると、座標リストから最初の値が抽出されます)。

これは次のようになります。

#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';

# open the coordinates file for reading
open(my $coord_fh, '<', 'coordinates.txt');

# read the file (line by line) into a List
my @coordinates = <$coord_fh>;

# close coordinate filehandle
close($coord_fh);

# open the other file for reading
open(my $other_fh, '<', 'otherfile.txt');

# save the lines you process
my @lines;

# first coordinate
my $coord = shift @coordinates;

# read line by line seraching for Coord
# replace with shift @coordinates if found
while ( my $line = <$other_fh> ) {
    if( $line =~ s/Coords/$coord/ ) {
        # get next coordinate
        $coord = shift @coordinates;
    }

    # save line
    push @lines, $line;
}

# close file for reading
close($other_fh);


# write all of the lines back to your file
open(my $out_fh, '>', 'otherfile.txt');

print {$out_fh} "$_" foreach(@lines);
于 2013-06-25T17:42:31.677 に答える