logo       

Re: Replacing a pattern in one file by reading a string from anotherfile: msg#00027

editors.sed.user

Subject: Re: Replacing a pattern in one file by reading a string from anotherfile


On Thu, 08 Dec 2005 23:46:11 +0800, <kaushik.m@xxxxxxx> wrote:

>>
>> Since you use awk and since you may want to get on more solution, I will
>> give my gawk solution:
>>

ARGIND is a built-in variable in gawk. It represents ARGument INDex, so
when we invoke awk this way:
awk -f f.awk f2 f3
"f2" has a argument index of 1 and "f3" has 2. When awk processing file,
it will set the ARGIND to the argument index of current file. That is to
say when awk is reading lines from "f2" the ARGIND would be 1. When awk
start to read lines from "f3" which in this case is the 2nd argument the
ARGIND would change to 2 accordingly.
When awk processing "f3" the ARGIND is set to 2 as a result the expression
(ARGIND-2) has a value of "ZERO" (false condition). If ARGIND is any number
other than 2, the expression (ARGIND-2) would has a non-zero value (ture
condition).

>> -------f.awk
# now you can see, if the ARGIND is 2,
# this block will be omitted
>> ARGIND-2{ a["$"$1]=$2
# by "next" command, gawk read next line from input, and
#the following commands (which is for "f3") would be
discarded.
>> next}

# only when ARGIND is 2 which means awk is reading lines from
# "f3" , the following commands would be executed.
# firstly check if the 2nd field is an index in array "a",
# if is do the substitution.
>> $2 in a{
>> sub("\\"$2,a[$2])}
>>
# No matter whether a substitution is taken or not, print the
# result. By default awk print the "$0" if awk does not find
# commands after a true condition. So if you change the "1"
# to anything non-zero, including "ARGIND-2" awk would print
# the result as well.
>> 1 # print the content of "f3".
>> -------end of f.awk
>>

the script can also be written as:
-------f.awk
BEGIN{ FS=": *" }

FILENAME=="f2"{
a["$"$1]=$2
}

FILENAME=="f3"{
if ($2 in a){
sub("\\"$2,a[$2])
}
print
}
-------end of f.awk
then invoke by:
awk -f f.awk f2 f3

--
Regards,
hq00e



------------------------ Yahoo! Groups Sponsor --------------------~-->
Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life.
http://us.click.yahoo.com/KIlPFB/vlQLAA/TtwFAA/dkFolB/TM
--------------------------------------------------------------------~->

--

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/sed-users/

<*> To unsubscribe from this group, send an email to:
sed-users-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/







<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise