19.03.2024, 06:35 UhrDeutsch | English
Hallo Gast [ Registrierung | Anmelden ]

Neues Thema eröffnen   Neue Antwort erstellen
Vorheriges Thema anzeigen Druckerfreundliche Version Einloggen, um private Nachrichten zu lesen Nächstes Thema anzeigen
Autor Nachricht
rich.bradshaw
Titel: Grep and Awk? Modifying text from command line.  BeitragVerfasst am: 21.11.2006, 16:41 Uhr



Anmeldung: 14. Jan 2006
Beiträge: 287

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
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
p500xl
Titel: RE: Grep and Awk? Modifying text from command line.  BeitragVerfasst am: 21.11.2006, 18:48 Uhr



Anmeldung: 05. Jul 2006
Beiträge: 127

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

_________________
astalavista == BSD is dying
http://talks.dixongroup.net/nycbsdcon2006/
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
h2
Titel: RE: Grep and Awk? Modifying text from command line.  BeitragVerfasst am: 21.11.2006, 20:16 Uhr



Anmeldung: 12. Mar 2005
Beiträge: 1005

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.

_________________
Read more on dist-upgrades using du-fixes-h2.sh script.
New: rdiff-backup script
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
rich.bradshaw
Titel: RE: Grep and Awk? Modifying text from command line.  BeitragVerfasst am: 21.11.2006, 20:45 Uhr



Anmeldung: 14. Jan 2006
Beiträge: 287

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!
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
rich.bradshaw
Titel:   BeitragVerfasst am: 21.11.2006, 22:13 Uhr



Anmeldung: 14. Jan 2006
Beiträge: 287

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!
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
rich.bradshaw
Titel:   BeitragVerfasst am: 22.11.2006, 12:49 Uhr



Anmeldung: 14. Jan 2006
Beiträge: 287

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
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
severin
Titel:   BeitragVerfasst am: 22.11.2006, 12:51 Uhr



Anmeldung: 18. Mar 2004
Beiträge: 3417

afair some echo option will do, as you suggested. try "man echo"

_________________
Kein Wort verstanden? Auf http://kanotix.com/index.php?module=pnW ... uerDummies
gibt's ein Glossar.

No clue what I'm talking about? New to Linux? Check http://kanotix.com/index.php?module=pnW ... ForDummies for a glossary.
 
 Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen ICQ-Nummer 
Antworten mit Zitat Nach oben
p500xl
Titel:   BeitragVerfasst am: 22.11.2006, 13:02 Uhr



Anmeldung: 05. Jul 2006
Beiträge: 127

Zitat:

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

enter key
echo "
1 => press enter
2 => press enter
" > my_file

_________________
astalavista == BSD is dying
http://talks.dixongroup.net/nycbsdcon2006/
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
rich.bradshaw
Titel:   BeitragVerfasst am: 22.11.2006, 17:26 Uhr



Anmeldung: 14. Jan 2006
Beiträge: 287

That easy eh! Thanks!
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
Beiträge vom vorherigen Thema anzeigen:     
Gehe zu:  
Alle Zeiten sind GMT + 1 Stunde
Neues Thema eröffnen   Neue Antwort erstellen
Vorheriges Thema anzeigen Druckerfreundliche Version Einloggen, um private Nachrichten zu lesen Nächstes Thema anzeigen
PNphpBB2 © 2003-2007 
 
Deutsch | English
Logos and trademarks are the property of their respective owners, comments are property of their posters, the rest is © 2004 - 2006 by Jörg Schirottke (Kano).
Consult Impressum and Legal Terms for details. Kanotix is Free Software released under the GNU/GPL license.
This CMS is powered by PostNuke, all themes used at this site are released under the GNU/GPL license. designed and hosted by w3you. Our web server is running on Kanotix64-2006.