2017-02-10 60 views
-1

值我有兩列[A,B]僅包含數字的.csv文件。 B列是「更長」的。桑達:刪除在相同csv文件

我想從塔B的每個數字,表示在列A中刪除

實施例:

A B 
1 1 
4 2 
5 3 
    4 
    5 

編號1,4,5應該從塔B,因爲它們存在於列被刪除A.

謝謝。

+0

您是否希望B列中的字段之後爲空,或者如果刪除發生,整列是否會向上移動? –

+0

如果發生刪除,整列將向上移位。謝謝。 – Sergiof4

+0

會更好,如果你可以添加樣品輸入/輸出突出各種情況,以便它可以進行測試..也可以嘗試自己解決...... https://stackoverflow.com/documentation/sed/topics一開始 – Sundeep

回答

1

鑑於此空間分隔的文件:

$ cat file 
A B 
1 1 
4 2 
5 3 
    4 
    5 

awk

awk 'NR==FNR {col1[++c1cnt]=$1; set[$1]; next} 
    $1 in set { next } 
    {col2[++c2cnt]=$1} 
    END { 
     m=(c1cnt<c2cnt) ? c2cnt : c1cnt 
     for (i=1; i<=m; i++){ 
       printf "%s\t%s\n", col1[i], col2[i] 
     } 
    }' <(awk 'NF==1 && /^[[:space:]]/ {next} {print $1}' /tmp/file) <(awk 'NF==2 {print $2; next} /^[[:space:]]/ {print $1}' /tmp/file) 
A B 
1 2 
4 3 
5 
0

@ Sergiof4:嘗試:

awk 'BEGIN{ 
      print "A B" 
     } 
FNR==NR && FNR>1 && $0 !~ /^[[:space:]]+/{ 
              A[$1]=$1; 
              C[++i]=$1; 
              next 
              } 
FNR!=NR && FNR>1{ 
        if(!A[$2]){ 
            ++k; 
            B[k]=$2 
           } 
       } 
END{ 
      for(j=1;j<=i;j++){ 
            print C[j] FS B[j] 
          } 
    } 
' Input_file Input_file 

上面的代碼是讀INPUT_FILE 2倍。我會在短時間內添加解釋。

EDIT2:添加對上面的代碼解釋過了。

awk 'BEGIN{              ##### starting BEGIN section here. 
       print "A B"          ##### printing the hearders now. 
      } 
    FNR==NR && FNR>1 && $0 !~ /^[[:space:]]+/{     ##### FNR(is a built-in awk's keyword which tells us number of lines in a Input_file, it gets RESET whenever it reads a new Input_file), NR(is also same as FNR only difference between them is NR's value will be keep on incressing till all Input_files are being read.). FNR==NR condition will be TRUE when first Input_file is being read. FNR>1 makes sure we are not reading header here. 0 !~ /^[[:space:]]+/ makes sure that our lines is not starting from space considering here we have only 2 fields in each line and empty space in starting of the line means it is having 1 field only in that line and do not want to have those lines(assumption here). 
               A[$1]=$1;   ##### creating an array A here whose index is $1 and it's value is $1, where $1 is the first field of each line of Input_file which is currently being read. 
               C[++i]=$1;  ##### creating an array C whose index is variable i whose value is incrementing each time above condition becomes TRUE and it's value is $1 of current line of Input_file. 
               next    ##### next is awk's in-built keyword which skips all next statements of this program. 
               } 
    FNR!=NR && FNR>1{           ##### This condition will be TRUE only when 2nd time Input_file is being read and it's current line is not first line. 
         if(!A[$2]){        ##### Checking if array A's value is NOT existing whose index is $2 of current line. 
             ++k;      ##### increasing the variable k's value to 1 each time. 
             B[k]=$2     ##### Assigning the array B's value to $2 whose index is variable k. 
            } 
        } 
    END{               ##### Starting END section for awk program now. 
       for(j=1;j<=i;j++){        ##### Starting a loop which starts from j=1 to till variable i's value. 
             print C[j] FS B[j]  ##### printing the values of array C and B which have their indexes as j. 
           } 
     } 
    ' file35 file35            ##### Mentioning the Input_file 2 times here as reading the Input_file 2 times. 

EDIT3:添加解決方案,其中甚至僅1場存在於一條線,但是應當然後採摘。起初我認爲不應該挑選,所以我沒有添加該邏輯代碼。

awk 'BEGIN{ 
       print "A B" 
      } 
    FNR==NR && FNR>1{ 
         A[$1]=$1; 
         C[++i]=$1; 
         next 
        } 
    FNR!=NR && FNR>1{ 
         if(!A[$2]){ 
             ++k; 
             B[k]=$2 
            } 
        } 
    END{ 
       for(j=1;j<=i;j++){ 
             print C[j] FS B[j] 
           } 
     } 
    ' Input_file Input_file 
+1

1)不可讀; 2)只適用於這個例子。試試'A = 1,4,5 B = 1,2,3,4,5,6,7' – dawg

+0

@dawg:謝謝你或你的反饋,讓我糾正它。對於不可讀的我正在寫它是非單線形式現在將添加它。 – RavinderSingh13

+0

@dwag:我已經添加了非一行解決方案以及代碼解釋,請告訴我是否可以對其進行一些有效的更改。 – RavinderSingh13