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

  • Home
  • SEARCH
  • 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 8433771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:28:46+00:00 2026-06-10T06:28:46+00:00

I have input data: foo 24 foobar 5 bar bar foo 125 and I’d

  • 0

I have input data:

foo 24
foobar 5 bar
bar foo 125

and I’d like to have output:

foo 024
foobar 005 bar
bar foo 125

So I can use this sed substitutions:

s,\([a-z ]\+\)\([0-9]\)\([a-z ]*\),\100\2\3,
s,\([a-z ]\+\)\([0-9][0-9]\)\([a-z ]*\),\10\2\3,

But, can I make one substitution, that will do the same? Something like:

if (one digit) then two leading 0
elif (two digits) then one leading 0

Regards.

  • 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-10T06:28:47+00:00Added an answer on June 10, 2026 at 6:28 am

    I doubt that the “if – else” logic can be incorporated in one substitution command without saving the intermediate data (length of the match for instance). It doesn’t mean you can’t do it easily, though. For instance:

    $ N=5
    $ sed -r ":r;s/\b[0-9]{1,$(($N-1))}\b/0&/g;tr" infile
    foo 00024
    foobar 00005 bar
    bar foo 00125
    

    It uses recursion, adding one zero to all numbers that are shorter than $N digits in a loop that ends when no more substitutions can be made. The r label basically says: try to do substitution, then goto r if found something to substitute. See more on flow control in sed here.

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

Sidebar

Related Questions

I have an input with the name attribute: <input type=text name=data[foo][bar] /> how can
I have a streaming input which has repeated values. I can use any data
I have to check hashrefs like this one { foo => 65, bar =>
I have an input edit field where the user can enter data. I want
I have some html like the following: <div class=control-group> <input type=text data-bind=value: $data.DealCode name=DealCode
Trying to merge some data that I have. The input would look like so:
How can I have additional data on an HTML element? For example, I'd like
I know I can do things like <input name=foo[]> , but is it possible
I have a function foo that takes a data frame as input and returns
I have the following data as input (sorted by first column): foo 1 2

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.