bash shell script find and edit fields in a file
Hi I am new to scripting. How do you find a particular field in a file and edit it? eg. in test.txt there are A, B and C fields but I only want to change the value of B. Then execute my program that read test.txt as input. After my program stop change value of B again, run my program again, stop program, change B and so on continuely.
In test.txt A = 1 B = 2 C = 3 change B = 4 execute program "test" after program stop change B = 5 execute program "test" again after program stop change B = 6 and so on... thanks alot |
sed or awk would work for this. I prefer sed since I already know it ;)
This looks like you're trying to do some testing on your program which is a great idea!! An understanding of regular expressions is important when working with these programs so if you don't know them, try to find more info on regular expressions first, then learn sed/awk. Look at the man pages of sed or awk to see more, but here's an example of sed to get you started: Code:
sed -r 's/B=[0-9]/B=5/' file.txt 's///' this is the structure of of sed expression. The string to match is between first two slashes and string to replace it with is between last two slashes file.txt is the input file |
really good tutorials here:
http://www.grymoire.com/Unix And good BASH manuals here: http://tldp.org |
how do the script know that prog had ended and loop again to change to value of B?
|
Run it in a loop with bash.
|
hi does sed really work? for eg. in test.txt i type "hello" and save.
then i run my script: echo hello | sed s/hello/bye/ test.txt its echo "bye" but when i cat test.txt its still "hello"? i need sometime that change the content of test.txt thanks |
Sed works as advertized. If you want to edit the file in-place, use "sed -i".
|
if you want to change value of B after executing "test" everytime, you need a way to store the value, such as into a file
Code:
# more file_num |
sed -i available in aix? got invalid flag -i error
thanks |
Possibly not. I use GNU sed. If the option is not available, you have to do it the hard way: redirect to a temporary file and then move that file to the original.
|
All times are GMT -5. The time now is 09:49 PM. |