Newbie Help required: Deleting double entries in a line ($0) 
Author Message
 Newbie Help required: Deleting double entries in a line ($0)

Dear Newsgroup,
i've got following file:
test1 one two three one four
test2 one three four four three
test3 three two one four two
.
.
.

The separator char is " " .
I want to delete double entries in the lines (e.g. for test1 the entry
"one").
I'm working on reliant unix 5.43
Have you any ideas?

Thx for answering
R. Hornung



Tue, 16 Sep 2003 04:02:35 GMT  
 Newbie Help required: Deleting double entries in a line ($0)

Quote:

> Dear Newsgroup,
> i've got following file:
> test1 one two three one four
> test2 one three four four three
> test3 three two one four two
> .
> .
> .

> The separator char is " " .
> I want to delete double entries in the lines (e.g. for test1 the entry
> "one").
> I'm working on reliant unix 5.43
> Have you any ideas?

> Thx for answering
> R. Hornung

I would use Awk's associative array capability and do something like
this:

{n=0; for(i=1; i<=NF; i++){if(!($i in a)) a[n++]=$i}
      for(i=0; i<n; i++) printf("%s ", a[i]); print "";
)

The first line sets consecutive entries in a[..] to the
unique elements of the line. (The test "$i in a" returns true (1) if
the array element a[$i] has been set and false (0) if not.)

The second line prints the uniquees on a single line separated by a
single space
(with an un-needed one at the end - you can fix this).
The array a[..] does not need to be deleted or initialized since
the index n keeps track, for each line, of what is used.

Martin Cohen



Tue, 16 Sep 2003 04:22:44 GMT  
 Newbie Help required: Deleting double entries in a line ($0)

Quote:

> i've got following file:
> test1 one two three one four
> test2 one three four four three
> test3 three two one four two

> The separator char is " ".
> I want to delete double entries in the lines (e.g. for test1 the
> entry "one").

You only need to loop through the words from left to right once,
printing each word the first time it occurs. Use an associative
array to keep track of which words have already been printed.

    {
        printf("%s", $1)  # print test1, test2, test3, etc.

        for (i = 2; i <= NF; i++) {
            if (!($i in seen)) {
                printf(" %s", $i)
                seen[$i]
            }
        }

        printf("\n")

        for (i in seen) {
            delete seen[i]
        }

        # delete seen  # this works with GNU awk and MKS awk
    }

--
Jim Monty

Tempe, Arizona USA



Wed, 17 Sep 2003 07:37:03 GMT  
 Newbie Help required: Deleting double entries in a line ($0)

Quote:

> test1 one two three one four
> test2 one three four four three
> test3 three two one four two
> I want to delete double entries in the lines (e.g. for test1 the entry
> "one").

Here is one idea (UNTESTED, apart from your example above)

{
   for (i=1; i<=NF; i++)
       if (++field[$i] == 1) printf("%s ",$i);
   printf("\n");
   delete field;

Quote:
}

--

"I'd rather write programs to write programs than write programs."


Wed, 17 Sep 2003 08:37:21 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. AWK: Help! Delete double entries in line ($0)?

2. mechanism of application ($0 or the line)

3. Embed points for double entry or templates for double entry bookkeeping

4. Delete all lines of a file (Newbie)

5. newbie Q: delete list few lines in file

6. bug(?): menu delete operation always deletes at least one entry

7. Newbie Q: Lining up entries

8. Entry Locater help required - can provide app

9. Help required on entries...

10. Appending to $0 or $_

11. variable as search-pattern: if ($0 ~ myVar)

12. executable awk scripts and shell var $(basename $0)

 

 
Powered by phpBB® Forum Software