View Single Post
Old 24th August 2010
s0xxx's Avatar
s0xxx s0xxx is offline
Package Pilot
 
Join Date: May 2008
Posts: 192
Default

Quote:
The #editing configs part is the only part left. So this is what I need to get this script to do now:

... snipped ...

I need the instances where it says 'WORD1' in both config files to be edited and replaced with the line I choose from the large file list. So say I want the word 'candy' to be placed where 'WORD1' is, I would run the script with the number '3' because 'candy' is the 3rd word in the list. But if I run the script again though (in a new terminal window), I have to make sure that 'candy' is replaced by the next word/line I choose because I can't have it hanging around from my previous use of the script.
If I understood correctly, this is what you're trying to achieve:
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; }
And here is the script to do it. Note it lacks error checks, and it doesn't edit the files, but rather print the output to stdout. Enough to get you started.
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
}
Script also doesn't delete the word from large_list (as you requested), but it should be fairly ease to do it.
__________________
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
Reply With Quote