Re: Adding prefixes
am 06.10.2007 00:20:59 von Ed Morton
Heruan wrote:
> I have a shell variable in a bash function like this:
>
> DOMAINS="abc.tld def.tld ghi.tld ..."
>
> I wish to add a prefix to all those domains and get:
>
> DOMAINS="host.abc.tld host.def.tld host.ghi.tld ..."
>
> How can I achieve this?
> Thanks in advance!
> Heruan
This is a shell question, not an awk one. Or, to put it another way,
awks not the best tool for this job. cross-posted and followups set to
comp.unix.shell.
Ed.
Re: Adding prefixes
am 06.10.2007 00:32:08 von Gretch
In news:r-ednfiN8tFRJZvanZ2dnUVZ_ubinZ2d@comcast.com,
Ed Morton wrote:
>> I have a shell variable in a bash function like this:
>>
>> DOMAINS="abc.tld def.tld ghi.tld ..."
>>
>> I wish to add a prefix to all those domains and get:
>>
>> DOMAINS="host.abc.tld host.def.tld host.ghi.tld ..."
>>
>> How can I achieve this?
>> Thanks in advance!
>> Heruan
>
> This is a shell question, not an awk one. Or, to put it another way,
> awks not the best tool for this job. cross-posted and followups set to
> comp.unix.shell.
DOMAINS="$(for i in $DOMAINS; do echo host.$i; done | paste -s -d" " -)"
>
> Ed.
Re: Adding prefixes
am 06.10.2007 00:34:19 von Stephane CHAZELAS
2007-10-05, 17:20(-05), Ed Morton:
> Heruan wrote:
>> I have a shell variable in a bash function like this:
>>
>> DOMAINS="abc.tld def.tld ghi.tld ..."
>>
>> I wish to add a prefix to all those domains and get:
>>
>> DOMAINS="host.abc.tld host.def.tld host.ghi.tld ..."
>>
>> How can I achieve this?
>> Thanks in advance!
>> Heruan
>
> This is a shell question, not an awk one. Or, to put it another way,
> awks not the best tool for this job. cross-posted and followups set to
> comp.unix.shell.
[...]
awk is not that bad a tool for it.
DOMAINS="abc.tld def.tld ghi.tld ..."
DOMAINS=$(
awk '
BEGIN {
$0 = ARGV[1]
for (i = 1; i <= NF; i++)
$i = "host." $i
print
exit
}' "$DOMAINS"
)
is an acceptably legible solution.
A more shell-like one could be:
DOMAINS=$(
printf '%s\n' "$DOMAINS" |
tr -s '[:blank:]' '[\n*]' |
sed -n 's/./host.&/p' |
paste -sd " " -
)
or for the shell-loop-no-fork-no-exec junkies:
IFS=" "
set -f
set -- $DOMAINS
for i; do
set -- "$@" "host.$1"
shift
done
DOMAINS="$*"
--
Stéphane
Re: Adding prefixes
am 06.10.2007 00:36:24 von Heruan
Gretch wrote:
> In news:r-ednfiN8tFRJZvanZ2dnUVZ_ubinZ2d@comcast.com,
> Ed Morton wrote:
>
>>> I have a shell variable in a bash function like this:
>>>
>>> DOMAINS="abc.tld def.tld ghi.tld ..."
>>>
>>> I wish to add a prefix to all those domains and get:
>>>
>>> DOMAINS="host.abc.tld host.def.tld host.ghi.tld ..."
>>>
>>> How can I achieve this?
>>> Thanks in advance!
>>> Heruan
>> This is a shell question, not an awk one. Or, to put it another way,
>> awks not the best tool for this job. cross-posted and followups set to
>> comp.unix.shell.
>
> DOMAINS="$(for i in $DOMAINS; do echo host.$i; done | paste -s -d" " -)"
Thank you! It's clean and works perfectly :)