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.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Print text on the same line after match by thanos1983
in thread Print text on the same line after match by periodicalcoder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.