Inserting a line from a ref. file..... 
Author Message
 Inserting a line from a ref. file.....

I have a small but difficult problem that needs your help.  I hope
somebody can show me how to do this in nawk.

I have these files.

**Current_File.txt
Student Allan
Seat Number 1
Examination Score    : 80
Test Started         : 13:00:00
Test Ended           : 13:45:52

Student Brandon
Seat Number 2
Examination Score    : 90
Test Started         : 13:00:00
Test Ended           : 13:53:55

Student Christy
Seat Number 3
Examination Score    : 85
Test Started         : 13:00:00
Test Ended           : 13:50:52

**Previous_Score_File.txt
Student Allan   Previous Score : 87
Student Brandon Previous Score : 82
Student Christy Previous Score : 93

I just want to get this output using a nawk script.

Student Allan
Seat Number 1
Examination Score    : 80
Student Allan   Previous Score : 87         #added
Test Started         : 13:00:00
Test Ended           : 13:45:52

Student Brandon
Seat Number 2
Examination Score    : 90
Student Brandon Previous Score : 82         #added
Test Started         : 13:00:00
Test Ended           : 13:53:55

Student Christy
Seat Number 3
Examination Score    : 85
Student Christy Previous Score : 93         #added
Test Started         : 13:00:00
Test Ended           : 13:50:52

Many thanks.
Guwapo

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 23 Feb 2003 05:13:49 GMT  
 Inserting a line from a ref. file.....
Sounds like homework to me.
Quote:

> I have a small but difficult problem that needs your help.  I hope
> somebody can show me how to do this in nawk.

> I have these files.

> **Current_File.txt
> Student Allan
> Seat Number 1
> Examination Score    : 80
> Test Started         : 13:00:00
> Test Ended           : 13:45:52

> Student Brandon
> Seat Number 2
> Examination Score    : 90
> Test Started         : 13:00:00
> Test Ended           : 13:53:55

> Student Christy
> Seat Number 3
> Examination Score    : 85
> Test Started         : 13:00:00
> Test Ended           : 13:50:52

> **Previous_Score_File.txt
> Student Allan   Previous Score : 87
> Student Brandon Previous Score : 82
> Student Christy Previous Score : 93

> I just want to get this output using a nawk script.

> Student Allan
> Seat Number 1
> Examination Score    : 80
> Student Allan   Previous Score : 87         #added
> Test Started         : 13:00:00
> Test Ended           : 13:45:52

> Student Brandon
> Seat Number 2
> Examination Score    : 90
> Student Brandon Previous Score : 82         #added
> Test Started         : 13:00:00
> Test Ended           : 13:53:55

> Student Christy
> Seat Number 3
> Examination Score    : 85
> Student Christy Previous Score : 93         #added
> Test Started         : 13:00:00
> Test Ended           : 13:50:52

> Many thanks.
> Guwapo

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sun, 23 Feb 2003 09:31:42 GMT  
 Inserting a line from a ref. file.....
gawk 'ARGIND==1{storage[$2]=$0;next};$1=="Seat"{print;print \ storage[doing};next};$1=="Student" {doing=$2};{print}' previous_file current_file

jennifer

Quote:

> I have a small but difficult problem that needs your help.  I hope
> somebody can show me how to do this in nawk.

> I have these files.

> **Current_File.txt
> Student Allan
> Seat Number 1
> Examination Score    : 80
> Test Started         : 13:00:00
> Test Ended           : 13:45:52

> Student Brandon
> Seat Number 2
> Examination Score    : 90
> Test Started         : 13:00:00
> Test Ended           : 13:53:55

> Student Christy
> Seat Number 3
> Examination Score    : 85
> Test Started         : 13:00:00
> Test Ended           : 13:50:52

> **Previous_Score_File.txt
> Student Allan   Previous Score : 87
> Student Brandon Previous Score : 82
> Student Christy Previous Score : 93

> I just want to get this output using a nawk script.

> Student Allan
> Seat Number 1
> Examination Score    : 80
> Student Allan   Previous Score : 87         #added
> Test Started         : 13:00:00
> Test Ended           : 13:45:52

> Student Brandon
> Seat Number 2
> Examination Score    : 90
> Student Brandon Previous Score : 82         #added
> Test Started         : 13:00:00
> Test Ended           : 13:53:55

> Student Christy
> Seat Number 3
> Examination Score    : 85
> Student Christy Previous Score : 93         #added
> Test Started         : 13:00:00
> Test Ended           : 13:50:52

> Many thanks.
> Guwapo

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sun, 23 Feb 2003 09:43:03 GMT  
 Inserting a line from a ref. file.....
On Wed, 06 Sep 2000 09:43:03 +0800, Jennifer Cross

Quote:


>> I have a small but difficult problem that needs your help.  
>> I hope somebody can show me how to do this in nawk.

>> I have these files.

>> **Current_File.txt

[snip]

Quote:
>> **Previous_Score_File.txt
>gawk 'ARGIND==1{storage[$2]=$0;next};$1=="Seat"{print;print \
>storage[doing];next};$1=="Student" {doing=$2};{print}' \
>previous_file current_file

Elegant.

--
  (  Kees Nuyt; Rotterdam; Netherlands

c[_] Disclaimer: Any opinions etc. are mine, not necessarily my employer's.



Sun, 23 Feb 2003 23:15:16 GMT  
 Inserting a line from a ref. file.....


Quote:
> On Wed, 06 Sep 2000 09:43:03 +0800, Jennifer Cross


> >> I have a small but difficult problem that needs your help.
> >> I hope somebody can show me how to do this in nawk.

> >> I have these files.

> >> **Current_File.txt

> [snip]

> >> **Previous_Score_File.txt

> >gawk 'ARGIND==1{storage[$2]=$0;next};$1=="Seat"{print;print \
> >storage[doing];next};$1=="Student" {doing=$2};{print}' \
> >previous_file current_file

> Elegant.

Elegant except that it is a gawk script NOT a nawk script as
requested.  The following is similiar but is a nawk script:

FILENAME~/^previous/ {prev_score[$2]=$0;next}
$1~/Student/ {student=$2}
{print}
$1~/Examination/ {print prev_score[student]}

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 24 Feb 2003 03:13:28 GMT  
 Inserting a line from a ref. file.....

Quote:



> > > I have a small but difficult problem that needs your help.  
> > > I hope somebody can show me how to do this in nawk.

> > gawk 'ARGIND==1{storage[$2]=$0;next};$1=="Seat"{print;print \
> > storage[doing];next};$1=="Student" {doing=$2};{print}' \
> > previous_file current_file

> Elegant.

Perhaps Guwapo will be kind enough to post the grade this script
earns.

I'd give it a C-. And Guwapo will get an even lower grade because
she obviously misunderstood, and thus mis-specified, the problem.

--
Jim Monty

Tempe, Arizona USA



Mon, 24 Feb 2003 08:01:59 GMT  
 Inserting a line from a ref. file.....

Quote:




> > > > I have a small but difficult problem that needs your help.

> > > gawk 'ARGIND==1{storage[$2]=$0;next};$1=="Seat"{print;print \
> > > storage[doing];next};$1=="Student" {doing=$2};{print}' \
> > > previous_file current_file

> > Elegant.

> Elegant except that it is a gawk script NOT a nawk script as
> requested. The following is similiar but is a nawk script:

> FILENAME~/^previous/ {prev_score[$2]=$0;next}
> $1~/Student/ {student=$2}
> {print}
> $1~/Examination/ {print prev_score[student]}

The usual idiom for processing the first input file differently
than the second and subsequent input files is

    NR != FNR { ... ; next }
                  { ... }

and as I explained in an article posted to comp.lang.awk last
year

    http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=512959442&fmt=text

it's not a good idiom.

Use BEGIN. That's what it's for.

Oh, by the way:

    $1 == "Student"

and

    $1 ~ /Student/

are not the same as

    $0 ~ /^Student/

Whether or not it matters here is moot. I'm just pointing out
another common mistake: using $1 == "foo" to match foo at the
beginning of a line.

--
Jim Monty

Tempe, Arizona USA



Mon, 24 Feb 2003 11:51:43 GMT  
 Inserting a line from a ref. file.....
Quote:

> The usual idiom for processing the first input file differently
> than the second and subsequent input files is

>     NR != FNR { ... ; next }
>                   { ... }

Cute..   not necessarily "usual" but then there are always lots of ways
of doing things (even when you're not using perl :)
Of course,using the gawk features, it is even more readable

Quote:
> and as I explained in an article posted to comp.lang.awk last year
>     http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=512959442&fmt=text
Ok, read that...

> it's not a good idiom.

What sort of database opereations were you doing for your test
where you justified this?  I can't think of a single scenario where
something using "harlan's trick" would be that much slower than
a procedure using your "Idiom".  (I agree harlan's systax would
be slower than the FNR test, but not _that_ much slower!!)

Quote:

> Use BEGIN. That's what it's for.

Thems fighting words son!  :)
I have lots of awk scripts with file name positional processing, and many of
them don't suit themselves to begin processing at all (would have to
duplicate large area of functionality into the BEGIN statement).
I use begin sometimes, and the state machine style processing in others.
One feature I REALLY miss is being able to put BEGIN and END in complex
action tests like:
  today != oldday || END { #dump & reset arrays }
Instead have to either use a subroutine and then call it from the condition
and seperately from END, or just cut and paste chunks around because it's easier.

Quote:

> Oh, by the way:

> Oh, by the way:

>     $1 == "Student"

> and
>     $1 ~ /Student/

> are not the same as

>     $0 ~ /^Student/

*nod* of course...  indeed $1 == "Student" is different than
$1 ~ /Student/ too. *shrug* would have worked with the stated
conditions.
Quote:

> Whether or not it matters here is moot. I'm just pointing out
> another common mistake: using $1 == "foo" to match foo at the
> beginning of a line.

yep. works just fine as match first word on a line when using the
default whitespace delimiters

Look forward to seeing the benchmark details!
Jennifer



Mon, 24 Feb 2003 15:09:17 GMT  
 Inserting a line from a ref. file.....


Quote:

>> The usual idiom for processing the first input file differently
>> than the second and subsequent input files is

SNIP some really good stuff ;-)
Quote:
>One feature I REALLY miss is being able to put BEGIN and END in complex
>action tests like:
>  today != oldday || END { #dump & reset arrays }
>Instead have to either use a subroutine and then call it from the condition
>and seperately from END, or just cut and paste chunks around because it's
> easier.

My way it to tag a dummy record at the end of the input file
one that triggers off that function

The tagging may be done by adding another filename or making a new
file e.g.

awk -f prog real-file one-line-dummy-file

copy real-file tempfile
echo DUMMY LINE >> tempfile
awk -f prog tempfile
del/rm tempfile

Mark
---
Mark Katz
Mark-it, London. Delivering MR-IT/Internet solutions
Tel: (44) 20-8731 7516, Fax: (44) 20-8458 9554
For latest information about ISPC/ITE - see http://www.e-tabs.com



Mon, 24 Feb 2003 19:29:51 GMT  
 Inserting a line from a ref. file.....

Quote:


> > and as I explained in an article posted to comp.lang.awk last year

> >     http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=512959442&fmt=text

> Ok, read that...

> > it's not a good idiom.

> What sort of database opereations were you doing for your test
> where you justified this?

Huh?

Quote:
> I can't think of a single scenario where something using "harlan's
> trick" would be that much slower than a procedure using your
> "Idiom". (I agree harlan's systax would be slower than the FNR
> test, but not _that_ much slower!!)

Who said anything about slower (i.e., execution speed)? Not I.

I said, "It is inefficient to test what file you're currently
processing at each input record." Which do *you* think is more
efficient: testing which of two one million-line files you're
currently reading zero times or two million times?

I was only pointing out a simple fact about good programming
practice, one that I think is fairly obvious. Am I missing something?

Quote:
> > Use BEGIN. That's what it's for.

> Thems fighting words son!  :)

I don't understand why.

Awk's implicit input loop functionality is wonderful and is one of
the many things we love about awk. (Right?) But in situations where
you need to process two or more files *differently*, you have to
work around this implicit loop. In some situations, it makes sense
to put the entire program in a BEGIN rule and forego the implicit
loop entirely. But for the often-encountered situation of needing
to first read an initialization file or smallish data file for the
purpose of, say, loading an array, it's better to do that in a
BEGIN rule and save the implicit loop for the "main" processing of
input data. Using tricks such as "NR != FNR" or "ARGIND == 1" (or
whatever) to overload the implicit loop mechanism means unnecessarily
repeating a test for *every* input record, which is inefficient
and not good programming practice.

Quote:
> I have lots of awk scripts with file name positional processing, and many
> of them don't suit themselves to begin processing at all (would have to
> duplicate large area of functionality into the BEGIN statement).

Use subroutines. That's what they're for. ;-)

Quote:
> Look forward to seeing the benchmark details!

Again, I don't understand this non sequitur about measuring the
comparative speed of execution of different scripts.

--
Jim Monty

Tempe, Arizona USA



Tue, 25 Feb 2003 01:11:26 GMT  
 Inserting a line from a ref. file.....

Quote:



> > > and as I explained in an article posted to comp.lang.awk last year

> > >     http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=512959442&fmt=text

> > What sort of database opereations were you doing for your test
> > where you justified this?

> Who said anything about slower (i.e., execution speed)? Not I.

Whoops! Yes, I did. I just re-read that old article and, in it,
I included "benchmarks". But they were jests. I was taunting
Harlan Grove who, at the time, was really into benchmarking. I
think the comparative times I posted were pretty clearly bogus.
Read <http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=513187235&fmt=text>.

Quote:
> Again, I don't understand this non sequitur about measuring the
> comparative speed of execution of different scripts.

I apologize. Now I see how my wisecracking misled you. My fault. I'm
sorry.

(I don't often benchmark awk scripts.)

--
Jim Monty

Tempe, Arizona USA



Tue, 25 Feb 2003 01:32:54 GMT  
 Inserting a line from a ref. file.....

<snip>

Quote:
>>Use BEGIN. That's what it's for.
>Thems fighting words son!  :)
>I have lots of awk scripts with file name positional processing, and
>many of them don't suit themselves to begin processing at all (would
>have to duplicate large area of functionality into the BEGIN
>statement).

Jim has a point when it comes to handling the first few of many input
files differently. Begin is cleaner, and if one's awk supports the -v
command line switch, then it's possible to avoid hardcoding the
filename or manipulating command line arguments in the BEGIN pattern.

For the more general case, if many files need to be processed
differently, an alternative is using the default pattern to call
different functions as opposed to state machine pattern-actions.

{ if (s == 1) do_1(); else if (s == 2) do_2(); ... }

vs

s == 1 { ...; next } s == 2 { ...; next } ...

Matter of taste.

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 25 Feb 2003 02:32:49 GMT  
 Inserting a line from a ref. file.....


...

Quote:
>> I can't think of a single scenario where something using "harlan's
>> trick" would be that much slower than a procedure using your
>> "Idiom". (I agree harlan's systax would be slower than the FNR
>> test, but not _that_ much slower!!)

>Who said anything about slower (i.e., execution speed)? Not I.

>I said, "It is inefficient to test what file you're currently
>processing at each input record." Which do *you* think is more
>efficient: testing which of two one million-line files you're
>currently reading zero times or two million times?

>I was only pointing out a simple fact about good programming
>practice, one that I think is fairly obvious. Am I missing something?

Jim is making an artistic point - something which is lost on most people.
In fact, most people can't even comprehend what is talking about.  The
closest most people can come to conceiving of the idea of a program being
"artistically correct" is to talk vaguely about "efficiency".  So, it should
come as no surprise that there seems to be an unbridgable communications
gap here.

I understand quite well what Jim is talking about, and I always strive to
make my programs artistically correct.  However, I actually disagree with
him on this particular point.  I frequently write programs that merge
several files and the code ends up looking like this:

        ARGIND == 1 { do_file_1;next }
        ARGIND == 2 { do_file_2;next }
        ARGIND == 3 { do_file_3;next }
        { do_final_file }

Yes, it does feel like you are making unnecessary comparisons, and that does
stick in my craw.  But, it is better than the alternatives.  I consider the
"read the lookup file(s) in the BEGIN clause" approach lazy for a variety of
reasons, including that you end up hard-coding the filename(s) into the
program (something which is never considered "recommended practice").  The
ARGIND approach allows the files to be specified on the command line (and
thus be settable/changeable at runtime).

I agree that the "NR == FNR" thing is ugly.  BTW, appropos of the discussion
of "array sorting", in which it was suggested (as I knew it eventually would
be) that if I like mawk so much, why don't I just use it (and leave the rest
of you God fearing GAWK users alone), note that mawk does not have an
"ARGIND" type variable and that's a reason not to use it (nor does it have
networking capabilities...)



Tue, 25 Feb 2003 02:50:52 GMT  
 Inserting a line from a ref. file.....


Quote:

...
>>        ARGIND == 1 { do_file_1;next }
>>        ARGIND == 2 { do_file_2;next }
>>        ARGIND == 3 { do_file_3;next }
>>        { do_final_file }
...
>The fact is, there are so many ways to avoid those needless
>"ARGIND == n" tests that it's a wonder even YOU can't figure out
>at least one of them.

If one needs information from previously read files when processing
file n, the only 'more efficent' approach that occurs to me is to use a
lot of getline loops:

{ do { do_file_1; getline } while (FNR > 1) }
{ do { do_file_2; getline } while (FNR > 1) }
{ do { do_file_3; getline } while (FNR > 1) }
{ do { do_final_file } while (getline > 0) }

What are the alternatives? There's no way to dispatch processing to
different user-defined functions without some sort of test or bypassing
the implicit input loop. [Off-topic: these situations may be better
suited to other languages.]

...

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 25 Feb 2003 07:23:54 GMT  
 Inserting a line from a ref. file.....


on reality, and ended with:
...

Quote:
>You're an imbecile. Go antagonize people in your TV gossip newsgroups
>and spare comp.lang.awk your ignorance of programming and the awk
>programming language.

Let me guess, you're just not getting any, are you?

Let me say just two things:
        1) Never in all my years of Usenetting have I seen someone so
totally lose it in a technical newsgroup.
        2) Since you are so proud of your deja skills, you might just like
to check out ASG yourself.  In particular, check out a couple of posters who
go by the names of "Emerald Eyes" and "Margaret Young", who are in the midst
of yet another iteration of a long-running online feud.  You'll fit right in.



Tue, 25 Feb 2003 08:28:43 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. inserting a blank line in a file

2. how to insert character at the beginning of each line in a file

3. insert several lines into a ascii-file

4. Inserting a Line in a DBF file

5. insert line in middle of file

6. How to insert a line in a file?

7. App-Ref: Need to break up a large App, App-Ref can help

8. Ref.: Re: Ref.: Re: Time.local bug?

9. list-ref <-> vector-ref

10. Sed: Inserting file in another file

11. Newbie: inserting an XML-file in an XML-file

12. to insert a blank line

 

 
Powered by phpBB® Forum Software