For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2005 > output to 2 files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author output to 2 files
chrishunnell@gmail.com

2005-05-13, 3:58 pm

Hi!

I have created a diff-file from two files and now I want to run an
awk-script to write the differences in two different files.

first file:
das ist ein testfile
und jede zeile wird mit einer anderen
verglichen
um die differenz zu berechnen
anschliessend muss man schauen welche
zeilen geupdated wurden
was ich mit dieser kleinen testfile machen
will

second file:
das ist ein testfile
und jede zeile wird mit einer anderen
verglichen
um die differenz zu berechnen
anschliessend muss man schauen welche
zeilen geupdated wurden
was ich mit dieser kleinen testfile machen
will

I have created to different diff-files but I am not quite sure which of
them is better to use with awk.

diff1:
*** sampl1 Fr Mai 13 08:41:32 2005
--- sampl2 Fr Mai 13 09:00:24 2005
***************
*** 1,8 ****
das ist ein testfile
! und jede zeile wird mit einer anderen
verglichen
um die differenz zu berechnen
- anschliessend muss man schauen welche
zeilen geupdated wurden
! was ich mit dieser kleinen testfile machen
will
--- 1,8 ----
das ist ein testfile
! und jed zeile wird mit einer anderen
verglichen
um die differenz zu berechnen
zeilen geupdated wurden
! was ich mit d ieser kleinen testfile machen
will
+ neue zeile


diff2:
2c2
< und jede zeile wird mit einer anderen
---
> und jed zeile wird mit einer anderen

5d4
< anschliessend muss man schauen welche
7c6
< was ich mit dieser kleinen testfile machen
---
> was ich mit d ieser kleinen testfile machen

8a8
> neue zeile



within the first file i can see exactly what changes there were between
the two files. With the awk I want to write all changed (update/new)
lines into one file and all deleted lines into another file. the main
problems are the updated lines because in diff1 they appear twice for
each line.

I hope you have a clue what i mean because it is hard to describe it.

Thanks for your help anyways!

Chris

chrishunnell@gmail.com

2005-05-13, 3:58 pm

I think it would be sufficient if it was possible to write all lines
with < in one file and all files with > in another!

Ed Morton

2005-05-13, 3:58 pm



chrishunnell@gmail.com wrote:
> I think it would be sufficient if it was possible to write all lines
> with < in one file and all files with > in another!
>


awk '{print > (/>/ ? "foo" : "bar")}'

Ed.
chrishunnell@gmail.com

2005-05-18, 8:55 am

Hi Ed,

thanks for your help!
Your script works out if all lines have > and < but not if it has other
lines to like "---".
The file I am manipulating is the output of a diff.
My code looks like that and does what it should :-)
It reads at the beginning the name of the input-file and then it tests
whether the line starts with > or < and then writes it into 2 different
files.
I guess there is a much shorter way to express the same :-)

BEGIN{
filename=ARGV[1]
}
{
if (/^\</)
{
out_del=filename"_d.txt"
gsub ("\< ", "")
print >> out_del

}
if (/^\>/)
{
out_update_new=filename"_un.txt"
gsub ("\> ", "")
print >> out_update_new
}
}
END{
}

Kenny McCormack

2005-05-18, 3:56 pm

In article <1116407978.306815.84220@g14g2000cwa.googlegroups.com>,
chrishunnell@gmail.com <chrishunnell@gmail.com> wrote:
>Hi Ed,
>
>thanks for your help!
>Your script works out if all lines have > and < but not if it has other
>lines to like "---".
>The file I am manipulating is the output of a diff.
>My code looks like that and does what it should :-)


When I saw Ed's post, I had the same reaction - that it assumed all the
lines started with < or > - i.e., that he had forgotten that you were
working with "diff" output.

How about :

match("<>",substr($0,1,1)) { print > ("file" RSTART) }

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com