LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2008, 04:05 PM   #1
hchoonbeng
LQ Newbie
 
Registered: Sep 2008
Posts: 15

Rep: Reputation: 0
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
 
Old 09-19-2008, 04:15 PM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
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
 
Old 09-19-2008, 05:18 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

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

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

Last edited by pixellany; 09-19-2008 at 05:19 PM.
 
Old 09-19-2008, 10:48 PM   #4
hchoonbeng
LQ Newbie
 
Registered: Sep 2008
Posts: 15

Original Poster
Rep: Reputation: 0
how do the script know that prog had ended and loop again to change to value of B?
 
Old 09-20-2008, 11:46 AM   #5
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Run it in a loop with bash.
 
Old 10-28-2008, 06:42 AM   #6
hchoonbeng
LQ Newbie
 
Registered: Sep 2008
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 10-28-2008, 07:03 AM   #7
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Sed works as advertized. If you want to edit the file in-place, use "sed -i".
 
Old 10-28-2008, 07:08 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 10-28-2008, 10:01 PM   #9
hchoonbeng
LQ Newbie
 
Registered: Sep 2008
Posts: 15

Original Poster
Rep: Reputation: 0
sed -i available in aix? got invalid flag -i error


thanks
 
Old 10-29-2008, 02:13 AM   #10
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need help create a bash script to edit CSV File imkornhulio Programming 13 02-05-2009 10:23 AM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 08:51 PM
Shell script to edit a file kaash_m31 Linux - Newbie 2 06-02-2008 05:43 AM
Bash script to edit text file snowman81 Linux - Desktop 2 01-10-2007 03:33 PM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 03:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration