Replace Data in a File
You have a file that has the following line in it:
#define MM_VERSION_STR "3.0a4"
How to find a way to set up a script that, when run,
will change the value of "3.0a4" to "3.0a5" and when it is run the next
time to "3.0a6" and so on?
Try this:
awk '{ if ($2 ~ /MM_VERSION_STR/) printf "%s %s %s%d\"\n",$1,$2,substr($3,1,5),substr($3,6,1)+1
; else print}' file
Or this:
awk '/MM_VERSION_STR/ { sub(/\"/,"",$3) ; $0 = sprintf("%s
%s \"%s%d\"",$1,$2,substr($3,1,4),substr($3,5)+1) } ; {print > ARGV[1]
}' file
...which can even count beyond 10 ;) - and it modifies
the file in place.
Replace /MM_VERSION_STR/ in the above by:
$1 == "#define" && $2 == "MM_VERSION_STR"
in case MM_VERSION_STR appears elsewhere in the same file.
See Also
Unix
Administrator Career Path
Have a Unix Problem
Do
you have a UNIX Question?
Unix Books :-
UNIX Programming,
Certification, System Administration, Performance Tuning Reference Books
Return to : - Unix
System Administration Hints and Tips
(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
All product names are trademarks of their respective
companies.
The site www.gotothings.com is in no way affiliated with
or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.
|