This is a discussion on Replacing text in a file using awk within the Shell scripting forums, part of the Development/Scripting category; Hey guys, i am about to throw it all in because i just cannot get this to work :S. I ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
Hey guys, i am about to throw it all in because i just cannot get this to work :S.
I am currently using fedora core 3. All i wanted to do is search through a file and find a string and replace with an argument from the command line. So basically i have a file which has the contents of : this is a test file yes i am cool say if i want to replace cool with $1 (which is 'crap', an argument from the shell) how do i accomplish this? So far i have $1 = awk '/yes/ { print $4 }' file > file I then want to overwrite the file with the updated contents. So the contents of file look like this: this is a test file yes i am crap This just doesnt seem to do the job. If anyone knows please help!! i wont have any fingernails left soon :P thanks guys. |
| Sponsored Links | ||
|
|
|
|||
|
Yeah thanks for your reply. I was thinking about using sed the other night and found out it was far easier to do what i was after than awk. However i have run into another problem.
How do i replace for example: BIAS UD=34 UG=20 so if i wanna replace UG=20 with UG=30 i can use sed 's/UG=20/UG=30/'' ...easy. But i am incrementing a UG from the command line so i need to change the UG occurrence already in the file with my new one ...eg: INC=40 sed 's/UG=*/UG='$INC'/' i need to be able to change UG=* in the file where star is any value...is there anyway to take star as wildcard instead of literally? sorry for my poor english. Its hard to explain my problem. |
|
||||
|
Few days back i had read something as follows posted by vivek (old forum) which basically changes the values of the config file
Code:
CF="/etc/test.config"
mod(){
NCF="$CF.$$"
[ ! -f $CF ] && touch $CF || :
V="$1"
grep -v -E "^$V=" $CF > $NCF
mv -f $NCF $CF
eval echo "$V=\"'\${$V}'\"">> $CF
}
# test data
host='me.test.com'
mod host
mod ip
It will create /etc/test.config as follows host='me.test.com' ip='' Again if you modify the script file with test data host='YOU.test.com' And script executed now output will be changed to as follows host='YOU.test.com' ip=''" Try this one or may be we can play more with sed .... |
|
|||
|
Thanks once again for your help. That is another way to accomplish the problem that i was having. However i have worked out a simple solution.
Where i was trying to substitute UG=* with UG="$INC" etc.....within shell i found you can use .* for all occurrences so i used UG=.*/UG="$INC" and presto....she works I have finally got the program working 100 % with error checking. thanks heaps |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| To find and replace in text file | nashtech | Shell scripting | 3 | 07-31-2008 07:46 PM |
| text output to mysql | chebbab | Shell scripting | 4 | 11-26-2007 08:29 PM |
| Cups is not printing text file with proper format, staircasing issue | Ashish Pathak | CentOS / RHEL / Fedora | 1 | 11-18-2007 09:12 AM |
| rearranging columns in a text file | sureshbup | Shell scripting | 2 | 12-06-2006 10:43 AM |
| Appending text files | sparky | Shell scripting | 13 | 04-03-2006 05:43 PM |