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 740653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:32:28+00:00 2026-05-14T08:32:28+00:00

EDIT : I don’t know in advance at which column my digits are going

  • 0

EDIT: I don’t know in advance at which “column” my digits are going to be and I’d like to have a one-liner. Apparently sed doesn’t do arithmetic, so maybe a one-liner solution based on awk?

I’ve got a string: (notice the spacing)

eh oh    37

and I want it to become:

eh oh    36

(so I want to keep the spacing)

Using awk I don’t find how to do it, so far I have:

echo "eh oh   37" | awk '$3>=0&&$3<=99 {$3--} {print}'

But this gives:

eh oh 36

(the spacing characters where lost, because the field separator is ‘ ‘)

Is there a way to ask awk something like “print the output using the exact same field separators as the input had”?

Then I tried yet something else, using awk’s sub(..,..) method:

' sub(/[0-9][0-9]/, ...) {print}'

but no cigar yet: I don’t know how to reference the regexp and do arithmetic on it in the second argument (which I left with ‘…’ for now).

Then I tried with sed, but got stuck after this:

echo "eh oh   37" | sed -e 's/\([0-9][0-9]\)/.../' 

Can I do arithmetic from sed using a reference to the matching digits and have the output not modify the number of spacing characters?

Note that it’s related to my question concerning Emacs and how to apply this to some (big) Emacs region (using a replace region with Emacs’s shell-command-on-region) but it’s not an identical question: this one is specifically about how to “keep spaces” when working with awk/sed/etc.

  • 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-05-14T08:32:28+00:00Added an answer on May 14, 2026 at 8:32 am

    Here is a variation on ghostdog74’s answer that does not require the number to be anchored at the end of the string. This is accomplished using match instead of relying on the number to be in a particular position.

    This will replace the first number with its value minus one:

    $ echo "eh oh    37      aaa     22    bb" | awk '{n = substr($0, match($0, /[0-9]+/), RLENGTH) - 1; sub(/[0-9]+/, n); print }'
    eh oh    36      aaa     22    bb
    

    Using gsub there instead of sub would replace both the “37” and the “22” with “36”. If there’s only one number on the line, it doesn’t matter which you use. By doing it this way, though, it will handle numbers with trailing whitespace plus other non-numeric characters that may be there (after some whitespace).

    If you have gawk, you can use gensub like this to pick out an arbitrary number within the string (just set the value of which):

    $ echo "eh oh    37      aaa     22    bb    19" |
        awk -v which=2 'BEGIN { regex = "([0-9]+)\\>[^0-9]*";
            for (i = 1; i < which; i++) {regex = regex"([0-9]+)\\>[^0-9]*"}}
            { match($0, regex, a);
            n = a[which] - 1;                    # do the math
            print gensub(/[0-9]+/, n, which) }'
    eh oh    37      aaa     21    bb    19
    

    The second (which=2) number went from 22 to 21. And the embedded spaces are preserved.

    It’s broken out on multiple lines to make it easier to read, but it’s copy/pastable.

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

Sidebar

Related Questions

EDIT: I don't want to delete the post because I have learned a lot
Can any one share some study material for multithreading on .net? EDIT: I don't
EDIT: I've found what's causing the issue, but I don't know why and I
I'm on a shared webhost where I don't have permission to edit the global
EDIT I don't know is it important, but destination triangle angles may be different
How to create an empty object in R? [edit: I don't know how to
Edit Make sure you don't juxtapose the request and response objects. things will be
EDIT 1 I apologize but after reading the 2 suggested articles I still don't
OKay I don't use actionscript and trying to help my friend edit a flash
My current solution will suck sometimes EDIT For those who don't understand,see this example:

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.