|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|
|||
Writing a simple script to edit text files and execute commands
Okay this will probably have multiple parts to it but I don't really want to trouble you guys with more help because I'm a total noob so I can just do the first part by hand (it's just editing a few hundred lines of text in a file; I have to do the same thing on each line and I'm sure there's a scripted solution for it but I can spare a couple hours).
So basically I have a file with several hundred lines, and each line is only 2-3 words. I need the script to do these things in order: 1) kill process1 2) put the 1st line of this file list into already existing text file #1 (requires root to write): the line it needs to write to is like: "option LINE1" where the 1st line has to be written where 'LINE1' is, somewhere in this text file 3) save this text file 4) put the 1st line of this file list into existing text file #2: the line is like "set = { opt1 = LINE1; opt2 = LINE1; opt3 = LINE1; }" and again I want the 1st line to be filled in all where 'LINE1' says 5) save text file 6) start process1 7) run a certain command in shell which will execute a CLI program and leave it running (I don't think it can be run in the background because I tried starting it with the '&' sign at the end and it ran but it didn't connect the way it properly was supposed to) 8) repeat but use the next line in the file list What programming language would be the quickest and easiest way to write something like this? It doesn't have to be fancy or anything, just work even if it's dirty |
|
|||
I would highly recommend you implement this as a shell script, & I would further suggest it be done as a Bourne shell script. Most shells (Korn, Z, Bash, etc.) at a bare minimum support Bourne shell conventions, so in many ways it is the least common denominator solution. Bourne shell scripts will minimal changes should run on most shells.
Last edited by ocicat; 24th August 2010 at 04:39 AM. Reason: updated tutorial link |
|
|||
That page says the guide is obsolete and is only preserved for historical purposes. Should I use the recommended guide linked on that page?
I have a general idea of how to stop/run processes because I do that every day, so I guess the main chunk of this is editing the text files and putting in the words where they belong. Would sed/awk work? Or will this not achieve the desired results because they are only stream editors and do one pass and are not state machines? (I remember the last time I worked with sed/awk to make mass changes to a text file, I was very unhappy with using such complicated syntaxes and stuff) Last edited by guitarscn; 24th August 2010 at 04:34 AM. |
|
|||
Quote:
Quote:
|
|
|||
How are your personal experiences with sed/awk? Generally between my friends and I, we all hate it for whatever reasons. I'm just not comfortable with it. I wonder what makes it so annoying to me.
|
|
|||
When it comes to shell programming, sed(1) & awk(1) are indispensible.
For example, here's code I use within a shell script configuring a USB drive as a install medium (so I don't have to continually burn CD's...). Note that I have numerous subshells exploiting sed(1) & awk(1): Code:
# ensure checksums match verify_checksums() { typeset FILE LOCAL SERVER ERROR=0 for FILE in * ; do if [ $FILE != SHA256 ] ; then echo $FILE | awk '{ printf "%-20s", $1 }' SERVER=$(egrep "^SHA256 \($FILE\)" SHA256 | awk '{ print $4 }') LOCAL=$(cksum -a sha256 $FILE | sed 's!^.* = \(.*\)$!\1!') if [ $LOCAL = $SERVER ] ; then echo 'OK' else echo 'not OK' ERROR=1 fi fi done [ $ERROR -ne 0 ] && panic 'SHA256 mismatch encountered' } |
|
||||
There's also cut(1), which is something of a poor man's awk. I've actually never used awk in any shell script.
For example this line: Code:
SERVER=$(egrep "^SHA256 \($FILE\)" SHA256 | awk '{ print $4 }') Code:
SERVER=$(egrep "^SHA256 \($FILE\)" SHA256 | cut -f 4 -d ' ') It should be pointed out that awk is much more powerful and can do much more than just simple selecting of columns. |
|
||||
Very similar here...I've only used awk *very* sparingly because I never really took the time to learn it.
sed is a different story. I've studied it and learned quite a bit of what it can do, and I find it absolutely indispensable. I have used sed to save jobs before, and that's no joke (corrupted plain-text database dump that was "cleaned up" with a sed one-liner so it could be properly restored). |
|
|||
Okay, I decided to make it a bit simpler, I have this so far:
Code:
#!/bin/bash killall process1 #editing configs process1 & sleep 5 process2 The large file is in this format: Code:
apple banana candy ... [several hundred lines of this] yoyo zebra Code:
option WORD1 otheroption blaha moreoption blahb Code:
name = { john } colors = { red,blue } options = { opt1 = WORD1; opt2 = WORD1; opt3 = WORD1; } |
|
|||
Quote:
http://www.shelldorado.com/shelltips...ml#oneinstance |
|
||||
Quote:
Code:
$ cat 1stconfig option WORD1 otheroption blaha moreoption blahb $ cat 2ndconfig name = { john } colors = { red,blue } options = { opt1 = WORD1; opt2 = WORD1; opt3 = WORD1; } $ cat large_file apple banana candy randy mandy yoyo zebra $ awk -f rep.awk 3 1stconfig: option candy 1stconfig: otheroption blaha 1stconfig: moreoption blahb 2ndconfig: name = { john } 2ndconfig: colors = { red,blue } 2ndconfig: options = { opt1 = candy; opt2 = candy; opt3 = candy; } $ awk -f rep.awk 4 1stconfig: option randy 1stconfig: otheroption blaha 1stconfig: moreoption blahb 2ndconfig: name = { john } 2ndconfig: colors = { red,blue } 2ndconfig: options = { opt1 = randy; opt2 = randy; opt3 = randy; } Code:
$ cat rep.awk BEGIN{ argument=ARGV[1] ARGC-- ARGV[ARGC++]="large_file" ARGV[ARGC++]="1stconfig" ARGV[ARGC++]="2ndconfig" } FILENAME=="large_file"{ if(NR==argument) word=$1 } FILENAME=="1stconfig"{ sub(/WORD1/,word,$2) print FILENAME": "$0 } FILENAME=="2ndconfig"{ gsub(/WORD1/,word,$0) print FILENAME": "$0 }
__________________
The best way to learn UNIX is to play with it, and the harder you play, the more you learn. If you play hard enough, you'll break something for sure, and having to fix a badly broken system is arguably the fastest way of all to learn. -Michael Lucas, AbsoluteBSD |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Request for sh script for compressing files | bgobs | Programming | 5 | 16th January 2010 07:20 PM |
execute commands from xinitrc | rex | FreeBSD General | 3 | 22nd October 2008 10:24 PM |
ruby execute commands remotely | Dr_Death_UAE | Programming | 0 | 18th August 2008 11:23 AM |
Cron won't execute a perl script | ivanatora | FreeBSD General | 4 | 17th August 2008 07:53 AM |
Create a script to rmove oldest files | disco | Programming | 5 | 14th July 2008 09:25 PM |