in reply to Print text on the same line after match
Hello periodicalcoder,
Fellow Monks have already answered your question. Just for fun this also a possible solution using rindex, substr and rename.
The script is written based on the assumptions of your statement. That there is a unique line ending with (~) and the numbers that you want to extract are 6. Other than that it should be really fast and efficient. Or at least I believe so :)
#!/usr/bin/perl use strict; use warnings; # you can enter as many files as you want as ARGV while (<>) { chomp; next if /^\s*$/; # skip empty lines if (rindex($_, "~") != -1) { my $number = substr $_, -7, 6; print $number ."\n"; rename $ARGV, $number . ".txt"; } } continue { close ARGV if eof; # Not eof()! } __END__ $ ls -la | grep txt -rw-rw-r-- 1 user user 78 Dec 15 00:32 in2.txt -rw-rw-r-- 1 user user 78 Dec 15 00:31 in.txt $ perl test.pl in.txt in2.txt 123456 654321 $ ls -la | grep txt -rw-rw-r-- 1 user user 78 Dec 15 00:31 123456.txt -rw-rw-r-- 1 user user 78 Dec 15 00:32 654321.txt __DATA__ $ cat in.txt Line 1 test in file 1 N1*PE*COMPANY NAME INC*XX*123456~ Line 3 test in file 1 $ cat in2.txt Line 1 test in file 2 N1*PE*COMPANY NAME INC*XX*654321~ Line 3 test in file 2
Hope this helps, BR.
|
|---|