Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Print text on the same line after match

by toolic (Bishop)
on Dec 14, 2018 at 20:35 UTC ( [id://1227266]=note: print w/replies, xml ) Need Help??


in reply to Print text on the same line after match

You can use capturing parentheses and print the captured number ($1) instead of the entire line:
perl -ne 'print $1 if /INC\*XX\*(\d+)/' cr835.txt 123456

Replies are listed 'Best First'.
Re^2: Print text on the same line after match
by periodicalcoder (Novice) on Dec 14, 2018 at 22:48 UTC
    Toolic, that did work, and thank you!

    One detail that I forgot to mention, and I edited the original post, is that I am working on Windows with Strawberry Perl. I had some trouble integrating the command into my script and could not store the output into a variable as it just went to stdout due to the "print $1 if" portion. I tried to redirect stdout, and not using "print" at all, but I ended up creating a temp file using this code to store the resulting number in a variable:

    use IO::Handle; # Extract the Payee ID from the file and store it into a temporary fil +e my $payeeid; system( q(perl -ne "print $1 if /INC\*XX\*(\d+)/" cr835.txt > payee.tx +t) ); # Define a file variable and read the contents to a variable my $file; $file = path("./payee.txt"); my $payeeid = $file->slurp; # Define the date and time for use in the new file name use constant DATETIME => strftime("%Y-%m-%d_%H-%M", localtime); # Define the new file name my $file = "CR835.txt"; my $newfilename = "CR835-" . $payeeid . "-processed_" . DATETIME . ".t +xt"; # Rename the file move $file, $newfilename;
    Is there a better way to store the result into the variable?
      Is there a better way to store the result into the variable?

      Yes - I showed how to do this without calling an extra perl process, it'll give you much more power and control.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found