LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash shell script find and edit fields in a file (https://www.linuxquestions.org/questions/programming-9/bash-shell-script-find-and-edit-fields-in-a-file-671123/)

hchoonbeng 09-19-2008 04:05 PM

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

CRC123 09-19-2008 04:15 PM

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
-r turns on extended regular expressions (you don't need it here, but it is useful sometimes

'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

pixellany 09-19-2008 05:18 PM

really good tutorials here:
http://www.grymoire.com/Unix

And good BASH manuals here:
http://tldp.org

hchoonbeng 09-19-2008 10:48 PM

how do the script know that prog had ended and loop again to change to value of B?

CRC123 09-20-2008 11:46 AM

Run it in a loop with bash.

hchoonbeng 10-28-2008 06:42 AM

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

burschik 10-28-2008 07:03 AM

Sed works as advertized. If you want to edit the file in-place, use "sed -i".

ghostdog74 10-28-2008 07:08 AM

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
4
# awk 'FNR==NR{num=$0;next}/B =/{print "B = "num;print ++num > "file_num" ;next}1' file_num file_contents
A = 1
B = 4
C = 3
# awk 'FNR==NR{num=$0;next}/B =/{print "B = "num;print ++num > "file_num" ;next}1' file_num file1
A = 1
B = 5
C = 3


hchoonbeng 10-28-2008 10:01 PM

sed -i available in aix? got invalid flag -i error


thanks

burschik 10-29-2008 02:13 AM

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.