Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Sed and Awk (http://www.programmingforums.org/forum22.html)
-   -   edit a text file (http://www.programmingforums.org/showthread.php?t=2521)

ojschubert Feb 28th, 2005 6:35 PM

edit a text file
 
Hi,

I'm a bit of a newbie so bare with me. I'd like to edit a number in a text doucment. The document looks like this:

row1: 20
row2: 40
row3: 49

I basically just want to be able to change these numbers. So far I've been able to get these numbers into a c-program by calling a shell script that uses grep and gawk. Now based on the results of my c-program I'd like to call another script that modifys the numbers in the txt document. I think I might need to use the grep and gawk again along with sed but am not really sure.

Can someone please help me get started out.

Thanks

OJ

sirclif Apr 14th, 2005 2:40 PM

how do you want to change the numbers? you to through each line and seperate the "row#" from the actual number pretty easy with gawk by setting the field seperator to ": "

gawk -F ": " -v OFS=": " '{print $1,$2}' textfile.txt

this tells gawk that the field seperator for each record (each line) is a colon and a space. the -F option sets the input field seperator. -v tells gawk that a variable is going to be set, in this case OFS (output feild seperator).

this line will just print out the textfile.txt. if you wanted to change the number to the line number you would use

gawk -F ": " -v OFS=": " '{print $1,NR}' textfile.txt

otherwise you'll have to do some decision making on how to change the second field, and insert it into the $2 slot.

hope this helps a little

Infinite Recursion Apr 18th, 2005 8:28 AM

how about using the string substitution of SED?


All times are GMT -5. The time now is 6:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC