Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Print text on the same line after match

by thanos1983 (Parson)
on Dec 14, 2018 at 23:55 UTC ( [id://1227282]=note: print w/replies, xml ) Need Help??


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.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1227282]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found