Reputation: 3229
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
Upvotes: 2
Views: 4094
Reputation: 58578
This might work for you:
cat makefile |
sed '/^\(version\|build\)/H;${x;s/^\(\nbuild.*\)\(\n.*\)/\2\1/;s/^.*=\(.*\)\n.*=\(.*\)/\1.\2/p};d'
0.8.2.1
tac makefile |
sed '/^\(version\|build\)/H;${x;s/^\(\nbuild.*\)\(\n.*\)/\2\1/;s/^.*=\(.*\)\n.*=\(.*\)/\1.\2/p};d'
0.8.2.1
If your not bothered if version comes before or after build, then:
sed '/^\(version\|build\)/H;${x;s/^.*=\(.*\)\n.*=\(.*\)/\1.\2/p};d'
0.8.2.1
Having read @outis solution, here is the ameliorated version of his solution for plainer viewing:
sed '/^version=/{s///;G;h};/^build=/{s//./;H};${g;s/\n//gp};d' makefile
Upvotes: 0
Reputation: 77450
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.
Upvotes: 5
Reputation: 77185
If you are open to awk
then here is another solution. Prevents the finger spasms caused by typing sed
code. No offence to the brilliant solutions offered by potong and kent
;)
awk -F"=" '
BEGIN{i=1}
(/version|build/){a[i]=$NF;i++}
END{for(i=1;i<=2;i++) if(i==1) {printf a[i]"."} else printf a[i]}' Makefile
Upvotes: 0
Reputation: 195289
try this with your makefile:
sed -rn -e '/^(version|build)/{s#^.*=##;H;}' -e'${x;s#\n##;s#\n#\.#;p}' makefile
test with your example text:
kent$ sed -rn -e '/^(version|build)/{s#^.*=##;H;}' -e'${x;s#\n##;s#\n#\.#;p}' <<<"foo=1
dquote> version=0.8.2
dquote> bar=2
dquote> build=1
dquote> bam=bug-AWWK
dquote> EOF"
0.8.2.1
in case build shows before version:
kent$ sed -rn -e '/^(version|build)/{s#^.*=##;H;}' -e'${x;s#\n##;s#\n#\.#;p}' <<<"foo=1
bar=2
build=1
bam=bug-AWWK
version=100.8.2"
1.100.8.2
Upvotes: 1