I have a text file with the following lines:

B        0.00000        0.00000       -2.71570
H        0.00000        1.18000       -2.71570
H       -1.02190       -0.59000       -2.71570
H        1.02190       -0.59000       -2.71570

using Perl how can I find "-2.71570" and increment it by -0.1 in duplicate files.

有帮助吗?

解决方案

What about something like this (assuming you're using bash):

dec=0.1; for i in {1..15}; do perl -ane 'BEGIN { $dec=shift }; print join( "\t", @F[0..2], $F[3]-$dec ), "\n"' $dec num_table.txt > file_$i.txt; dec=$(bc <<<$dec+.1); done

That will start with the decrement value of 0.1, and use a shell loop to create 15 new files with the last field modified by the decrement value, which is increased by 0.1 each time. I'm not completely clear on your desired format, but maybe this will get you started.

其他提示

In a perl 1-liner

perl -pe "s/([-\d.]+)$/$1 -.1/e" foo.txt > foo.txt.1

Now just duplicate 14 more times, and you'll have 15 files.

Or use the following script to create 15 files at once.

perl -lne '
    BEGIN {@f=map {open my $fh,q{>},qq{$ARGV[0].$_};$fh} (1..15)}
    for $i (0..$#f) {s/([-\d.]+)$/$1-.1/e; print {$f[$i]} $_}
    ' foo.txt
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top