Computing.Net > Forums > Unix > replace data in a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

replace data in a file

Reply to Message Icon

Name: gnussbaum
Date: April 26, 2004 at 11:39:20 Pacific
OS: Win2K and CYGWIN
Comment:

I have a file that has the following line in it:
#define MM_VERSION_STR "3.0a4"

I'm trying 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...I'm thinking that awk and sed could do this but am not familiar with them. Any ideas??



Sponsored Link
Ads by Google

Response Number 1
Name: Wolfbone
Date: April 26, 2004 at 14:46:53 Pacific
Reply:

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


0

Response Number 2
Name: Wolfbone
Date: April 26, 2004 at 20:40:17 Pacific
Reply:

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.


0

Response Number 3
Name: Wolfbone
Date: April 26, 2004 at 21:27:35 Pacific
Reply:

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.


0

Response Number 4
Name: gnussbaum
Date: May 11, 2004 at 11:23:18 Pacific
Reply:

Thanks Wolfbone!

Still trying to figure this awk stuff out! I'm confused about if the field you want to replace is on another line. For example:

resource 'tver' (1000) {
"3.0a6"
};

How would I change the "3.0a6" to "3.0a7" and so forth like above. Since the field is on the second line I'm not sure??


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Help Unix Script passing echo string over ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: replace data in a file

Replacing values in a file www.computing.net/answers/unix/replacing-values-in-a-file/7683.html

how to replace a line in a file www.computing.net/answers/unix/how-to-replace-a-line-in-a-file/7214.html

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html