Script to generate names
Parv
parv at pair.com
Sat Feb 4 13:36:49 PST 2006
in message <20060204053659.GA2174 at holestein.holy.cow>,
wrote Parv thusly...
>
...
> in_1="list1"
> in_2="list2"
> save="list3"
>
> [ -f "$save" ] && mv -f "$save" "$save--OLD"
> {
> while read word_1
> do
> while read word_2
> do
> printf "%s%s\n%s%s\n" \
> "$word_1" "$word_2" \
> "$word_2" "$word_1"
>
> # If all the possible combinations of all the words are
> # needed, remove or comment out the following "break".
> break
>
> done <"$in_2"
> done <"$in_1"
> } | sort -u >> "$save"
>
This is just broken at "break" as you would get combination of all
the words from "$in_1" only w/ the first word in "$in_2". Remove
"break", and code works to generate all the possible combinations.
With "break" there, i was going for line wise pairs: word from line 1
from file 1 is paired w/ word from line 1 from file 2; word from
line 2 from file 1 paired w/ word from line 2 from file 2.
Of course, i forgot that that will cause the file 2 to be read from
the start. What is needed is (involving use of fseek(3) & ftell(3))
...
- open file 2 to read; save the current position, say in variable pos
- inside the loop reading file 1, ...
+ read a word from line 2 starting at $pos
+ print word pairs
+ overwrite $pos w/ the current position in file 2
In any case, below is the version to generate all the pairs (does
not emits line wise pairs as stated above) ...
#!/bin/sh
in_1="list1"
in_2="list2"
save="list3"
[ -f "$save" ] && mv -f "$save" "$save--OLD"
{
while read word_1
do
[ -z "$word_1" ] && continue
while read word_2
do
[ -z "$word_2" ] && continue
printf "%s%s\n%s%s\n" \
"$word_1" "$word_2" \
"$word_2" "$word_1"
done <"$in_2"
done <"$in_1"
} | sort -u >> "$save"
exit
- Parv
--
More information about the freebsd-questions
mailing list