Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying text files with unix script

Status
Not open for further replies.

ezeke1

Programmer
Mar 20, 2003
41
US
I have a handful of source files written in C code and I would like to change the header information in all of them. So basically I want to either add or remove the text in the file and replace it with something I want. What is the best approach to accomplish this? Here's an example:

"test.c" file:

////////////////
// old header //
// //
////////////////


Thanks all!!!
-David
////////////////
// new header //
// //
////////////////
 
man sed

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
ezeke1,

For your example, try something like this:

sed 's!// old header //!// new header //!g' test.c > $$.tmp && mv $$.tmp test.c

Hope this helps.

John
 
Thanks John and PH, let me give sed a try.

-David
 
Hi, All:
Although we can do pattern match and corresponding substitution in Awk and Sed, I find it very disable when we want to do value-substitution.I mean:say, when I want to replace the words which equal to the value of $var1(say,$var1="charlie", we want to replace where it is charlie to daniel,here $var2=daniel) in file "file_test1",and we can't know what string to replace because var1,var2 is dynamically assigned value.How to do such "value-substitution"??? e.g.:
sed 's/$var1/$var2/g' file_test1
this will fully disobey what we want it to do!!!it will do pure pattern substitution,$var1--->$var2, but we want it to replace all "charlie" ---->"daniel".
PLEASE TELL ME HOW TO SOLVE SUCH A PROBLEM???We have to do such work in our work, THIS IS IN VERY URGENT NEED!!!!
help,please.
 
If neither $var1 nor $var2 contains '/' characters:
Code:
sed 's/[highlight]'"[/highlight]$var1[highlight]"'[/highlight]/[highlight]'"[/highlight]$var2[highlight]"'[/highlight]/g' file_test1

--------------------

Denis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top