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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:25:04+00:00 2026-05-27T16:25:04+00:00

I have a Makefile , that has variables version and build (those are not

  • 0

I have a Makefile, that has variables version and build (those are not the only variables, and can be defined in different order). Using only sed, I’d like to combine the values into a single version string.

So far I’ve got:

sed -n -e '/=/{s/version.*=\(.*\)/\1\./p;s/build.*=\(\d*\)*/\1/p}' Makefile

but both values are separated by newline. The above produces following output:

0.8.2.
1

I’d like:

0.8.2.1

I tried N, but couldn’t figure how to use it in this sitation.

Why ‘sed only’ restriction? I’d like to learn it, and that is the best way for me.

Standalone sample:

sed -n -e '/=/ {s/version.*=\(.*\)/\1\./p;s/build.*=\(\d*\)*/\1/p}' <<EOF
foo=1
version=0.8.2
bar=2
build=1
bam=bug-AWWK
EOF
  • 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-27T16:25:04+00:00Added an answer on May 27, 2026 at 4:25 pm

    The following are all based on the same idea: store the version and build numbers, then print them at the end of input.

    When it comes to storage, sed has the pattern space, which starts with the value of the current line, and a hold space, which can be used to save values for the duration of the process. The version should be wind up prepended to the value in the hold space, which can be accomplished by appending the hold space to the pattern space with G. The build should be appended to the hold space, which can be done with H. To remove the newline that H creates, the hold space is moved back to the pattern space. In both cases, the newline created by the G and H is removed with a s///, then back to the hold space with h. The end of input is signified by the $ address, at which the hold space is moved back into the pattern space and printed. It’s shorter to see:

    sed -n -e '/^version.*=/ {s/[^=]*=//; G; s/\n//; h; } ' \
           -e '/^build.*=/ { s/[^=]*=//; H; g; s/\n/./; h; } ' \
           -e '$ { g; P; }'
    

    This produces a newline at the end of the string, but hopefully that won’t be an issue.

    The awk family of utilities support variables, making the task more straightforward. They have the special variables OFS, the output field separator, and ORS, the output record separator. awk’s print outputs the OFS between each argument, and ORS at the end. The special pattern END matches after the end of input.

    awk -v OFS=. -v ORS='' \
        '/^version.*=/ {sub(/^[^=]*=/, "");  version=$0;} \
         /^build.*=/ {sub(/^[^=]*=/, ""); build=$0;} \
         END {print version, build;}'
    

    If you don’t care about the trailing dot and can be certain of the order of the version and build lines in the makefile:

    awk -v ORS=. '/(version|build).*=/ { sub(/^[^=]*=/, ""); print; } '
    

    Continuing upwards in expressiveness of language is perl. perl’s END has a similar function to awk’s, marking a block to be run at the end of the process. A hash can be used to store the parts of the complete version number, allowing the lines to match and store the version and build to be combined into a single line.

    perl -n -e '$ver{$1} = $2 if /^(version|build)[^=]*=(.*)/; \
                END {$,="."; print @ver{"version","build"}};'
    

    While using sed makes for an interesting exercise, the value in exercises is strengthening yourself by doing them. If you must turn to others, it’s better not to focus on how you’re trying to solve the problem but instead find out recommended approaches. You’ll learn more.

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

Sidebar

Related Questions

I have a section of makefile that has this sort of structure: bob: ifdef
I have a project that has a makefile with broken dependencies. Is there any
I have a makefile project that builds and links a DLL, using the command-line
I have a Makefile for a C program that has the declaration CC?=gcc Changing
I have a Makefile that only compiles/recompiles what's necessary. However, even after something is
I have the following makefile that I use to build a program (a kernel,
I have a Qt project and a subfolder libparanoia that has it's own Makefile
I have a Makefile that looks like this CXX = g++ -O2 -Wall all:
I have a working makefile that builds with mingw32. Now i renamed that makefile
I have some demos that I downloaded and they come with a Makefile.win and

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.