Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8029613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:29:22+00:00 2026-06-05T00:29:22+00:00

I have a program that when I run, it prints something like this in

  • 0

I have a program that when I run, it prints something like this in command line:

file test.test: 427 sentences, 2433 words, 1186 OOVs
0 zeroprobs, logprob= -4914.55 ppl= 862.603 ppl1= 8731.65

But I only want to save the number 862.603 in an environment variable. How can I extract that single number from output of the program?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:29:24+00:00Added an answer on June 5, 2026 at 12:29 am

    Every answer I’ve seen thus far has had some deficiency, so I guess I’ll go ahead and add this to the mix:

    There are a couple of ways to do this.

    My preferred way of doing it would be with grep’s perl extension (-P):

    var=$(myProgram | grep -oP 'ppl=\s*\K\d+\.\d+')
    

    The -o flag tells grep to only print the matching string, which in this case is the number you’re looking for.

    Note that this is (almost) identical to sputnick’s solution, but I’ve adapted it to read from your program directly via a pipe rather than a from a file.

    I prefer that solution because you’re essentially wanting to search for a particular string, which grep excels at. The only problem is you also want to do a lookbehind, which is only supported in the perl regex extension.

    So, if your grep doesn’t support the perl regex extension, I would use sed:

    var=$(myProgram | sed 's/ppl=\s*\(\d\+\.\d\+\)/\1/')
    

    This assumes gnu sed, which is pretty common. If you don’t have gnu sed, then use this:

    var=$(myProgram | sed 's/ppl=[ \t]*\([0-9]\{1,\}\.[0-9]\{1,\})/\1/')
    

    The bottom line here is, you absolutely do not need more than one pipe to accomplish this task. Opening pipes mean starting new processes, which is expensive. In general, you want to open up as few pipes as necessary to accomplish your task when coding in the shell.


    Edit

    Just to point it out: sputnick’s answer is now pretty much exactly what you want if you have the perl extension available. The only difference between his and mine now is a small regex change (which you’ll likely have to tweak yourself to suit your needs in the end anyways).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple test program that when I run I get: ./hello: error
I have a java program that is supposed to run a bat file, and
I have a python program/file that I want to run repeatedly and calculate the
I have a program that only allows one instance of itself to run. I
I have a client who wants a small database program that can run on
I've run into an rather irritating problem in java. I have a program that
I have a legacy program that I need to run with the extension of
I have made a small program in C# that I want to run in
I have a requirement such that my program is suppose to run for a
I have a table and I want that everytime I run the program it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.