共通要素の列を並べ替えますが、他の要素に独自の行を提供する方法は何ですか?

共通要素の列を並べ替えますが、他の要素に独自の行を提供する方法は何ですか?

私はこれを使ってpaste3つのテキストファイル(ソートする必要はありません)を3つの列を持つ1つの文書にマージします。

paste a.txt b.txt c.txt

私は、列に共通の要素が一致しない要素(現在はそうです)と共有せずに同じ行を占めたいと思います。同様に、一意の要素には独自の行が必要です。各列の要素は元の順序を維持する必要があります。

これは簡単な例です。

入力する

1 1 1
2 2 2
3 4 4
5 5 5
1 1 2
3 3 3

希望の出力

1 1 1
2 2 2
3    
  4 4
5 5 5
1 1
    2
3 3 3

以下はより複雑な例です。

入力する

000 000 000
002 002 001
006 006 006
008 008 007
009 009 009
011 012 010
013 013 013
015 015 014
016 016 016
018 019 017
020 020 020
021 021 022
024 024 024
026 025 025
028 026 026
118 028 027
119 118 118
032 119 117
036 032 032
037 033 033
039 034 034
040 037 037
042 039 038
043 040 040
045 042 041
046 043 043
048 045 044
    046 046
    049 047

希望の出力

000 000 000
        001
002 002
006 006 006
        007
008 008 
009 009 009
        010
011        
    012 
013 013 013
        014
015 015 
016 016 016
        017
018     
    019 
020 020 020
021 021 
        022
024 024 024
    025 025
026 026 026
        027
028 028 
118 118 118
        117
119 119 
032 032 032
    033 033
    034 034
036     
037 037 037
        038
039 039 
040 040 040
        041
042 042 
043 043 043
        044
045 045 
046 046 046
        047
048     
    049

理想的には、Linux/Unixに組み込まれているツールを使用したいと思います。また、出力を3つの列を持つ単一の文書として保持したいと思います> whatever.csv

私が得ることができる最も近いのは、元のsdiffテキストファイルで実行されることでしたが、ファイル内で一緒に共有された要素が正しくソートされましたが、私が望む方法で違いを処理できませんでした。

ベストアンサー1

BEGIN {
    # We assume the default input field separator (changeable with "-F")
    # Output will be tab delimited.
    OFS = "\t"
}
{
    # The number of output records that this input record results in.
    k=0

    # "seen" records which new record a field should be part of.
    # There may be NF new records for each input record if all
    # fields are unique.
    delete seen

    # "a" holds all data for the new output records.
    # It's basically a 2-dimensional NFxNF matrix
    # encodod in a 1-dimensional array.
    delete a

    # Iterate over the fields
    for (i=1; i<=NF; ++i) {
        if (!seen[$i]) {
            # This data has not been seen before (in this input record),
            # assign it to the next output line.

            seen[$i] = ++k
        }

        # Assign the input field to the right spot
        a[(seen[$i]-1)*NF + i] = $i
    }

    # Save NF as this is reset by emptying $0 later.
    nf = NF

    # Create and output new lines
    for (j = 1; j<=k; ++j) {
        $0 = ""

        # Create new output record
        for (i = 1; i<=nf; ++i)
            $i = a[(j-1)*nf + i]

        # Output record
        print
    }
}

与えられたデータのテスト:

$ awk -f script.awk file
1       1       1
2       2       2
3
        4       4
5       5       5
1       1
                2
3       3       3

他のデータのテスト:

$ cat file
a b c e
1 2 1 1
2 1 1 1
1 1 1 2
$ awk -f script.awk file
a
        b
                c
                        e
1               1       1
        2
2
        1       1       1
1       1       1
                        2

おすすめ記事