kanotix.com

Anything goes - Grep and Awk? Modifying text from command line.

rich.bradshaw - 21.11.2006, 16:41 Uhr
Titel: Grep and Awk? Modifying text from command line.
Does anyone know how to change values (first column) in a file structured like this using grep, awk or some other command line way?

Code:
1 #Variable1
2 #Variable2
3 #Variable3


I can use
Code:
grep Variable1 file.ini
to find instances of each variable, but can't find a good way to implement it...

Also, how do I write a loop in Bash to loop that code, and change it to different values? Ideally I want to change the ini file, run a program. Change the ini file, run the program loads of times to produce different results so I can analyse them. If anyone is interested I'm simulating the flow of information through ant colonies, and want to alter the initial conditions...

Any ideas?

Thanks a lot,

Rich
p500xl - 21.11.2006, 18:48 Uhr
Titel: RE: Grep and Awk? Modifying text from command line.
don't know if i understand you right
but thought sed is your friend
have a look at
TEXT CONVERSION AND SUBSTITUTION
http://www.student.northpark.edu/pement ... d1line.txt
h2 - 21.11.2006, 20:16 Uhr
Titel: RE: Grep and Awk? Modifying text from command line.
sed is your friend, but you need to give more information, what is the first column being changed TO?

1 #Variable1

is 1 supposed to change to something else depending on which VariableX you have in the column?

You have to give more details, not general stuff, but an actual example of the desired outcome with a sample string input.
rich.bradshaw - 21.11.2006, 20:45 Uhr
Titel: RE: Grep and Awk? Modifying text from command line.
Sorry, no column 1 just changes to a different value, e.g.

1 #Inital Speed
2 #Inital Angle

might change to

2 #Initial Speed
4 #Initial Angle

I want to automate the setting up (changing this file) and running a program basically. Sorry my explanation wasn't so good! Smilie

I'll have a look at sed, thanks!
rich.bradshaw - 21.11.2006, 22:13 Uhr
Titel:
I ended up doing it like so:

Code:
X=0
while [$X -le 100]
do
echo "1 #initial speed" > initialisation.ini
echo "$X #initial angle" >> initialisation.ini
echo "Datafile${X}.csv #initial angle" >> initialisation.ini
echo "all the other variables etc..." >>initialisation.ini

program

done


which seems to work fine... is there a better way of doing this? It seems pointless to rewrite the file everytime I change it, but this does work!

sed looks complex!
rich.bradshaw - 22.11.2006, 12:49 Uhr
Titel:
How do you echo something with a new line in it? e.g. I want to write all those lines to the file, and have resorted to doing it in lots of echo commands, is there a way to get echo to write a carriage return? I tried echo "Line 1/nLine2/nLine3", but that doesn't work... must be easy I imagine, but can't work it out!

Rich
severin - 22.11.2006, 12:51 Uhr
Titel:
afair some echo option will do, as you suggested. try "man echo"
p500xl - 22.11.2006, 13:02 Uhr
Titel:
Zitat:

is there a way to get echo to write a carriage return?

enter key
echo "
1 => press enter
2 => press enter
" > my_file
rich.bradshaw - 22.11.2006, 17:26 Uhr
Titel:
That easy eh! Thanks!
Alle Zeiten sind GMT + 1 Stunde
PNphpBB2 © 2003-2007