如何从一个文件中搜索字符串并在Linux中更新到另一个文件


نسر الصحراء
2025-03-18 05:43:26 (21天前)


我有两个文件,我从一个文件中逐个搜索字符串,并将记录更新为另一个文件。说我有文本文件A.csv

TABLE1,ABC_STRING
TABLE2,ABC_STRING
表3,ABC_STRING
B.csv



4 条回复
  1. 0# 無口君 | 2019-08-31 10-32



    因为unix




    1. cat <A.csv
      TABLE1, ABC_STRING
      TABLE2, ABC_STRING
      TABLE3, ABC_STRING
      EOF

    2. cat <B.csv
      TABLEA,SOMEVALUE,ABC_STRING,NULL,ABC_STRING
      TABLEB,SOMEVALUE,ABC_STRING,NULL,ABC_STRING
      TABLE1,SOMEVALUE,ABC_INT,NULL,ABC_INT
      TABLEC,SOMEVALUE,ABC_STRING,NULL,ABC_STRING
      TABLE2,SOMEVALUE,ABC_INT,NULL,ABC_INT
      TABLE3,SOMEVALUE,ABC_INT,NULL,ABC_INT
      EOF

    3. join the first file on the first field

      with the second file on the second field

      print unmatched lines from the second file

      the unknown matches are substituted with

      the output is complicated - we output the matched correct line (6 fields)

      and after it we output the original second file (6 fields)

      when there is no match, the last field from the correct line

      is the empty separator ‘##’

      we can filter is later with sed

      join -11 -22 -t, -a2 -e’##’ -o 2.1,2.2,2.3,1.2,2.5,1.2,2.1,2.2,2.3,2.4,2.5,2.6 <(

    4. # dunno what the spaces are doing in A.csv, remove them
    5. <A.csv tr -d ' ' | 
    6. # sort the file on the first field
    7. sort -k1 -t,
    8. ) <(

    9. # add a number of the lines to the second file
    10. # so we can sort it like the original file later
    11. <B.csv nl -s, -w1 | 
    12. # sort it on the second field (the first field is the number now)
    13. sort -k2 -t,
    14. ) |

    15. here the output looks like:

      6,TABLE3,SOMEVALUE,ABC_STRING,NULL,ABC_STRING,6,TABLE3,SOMEVALUE,ABC_INT,NULL,ABC_INT

      1,TABLEA,SOMEVALUE,##,NULL,##,1,TABLEA,SOMEVALUE,ABC_STRING,NULL,ABC_STRING

      remove the first 6 fields from the lines with ##, they were not matched

      sed ‘s/.*,##,//‘ |

    16. extract first 6 fields, less to sort, operation is cheap

      cut -d, -f1-6 |

    17. sort on the field numerical. This is the numbers we inserted in the second file

      sort -k1 -t, -n |

    18. extract 5 lines from the original

      cut -d, -f2-6

    19. </code>

  2. 1# 昵称不能为空 | 2019-08-31 10-32




    1. awk -F’, *’ -v OFS=’,’ NR==FNR{m[$1]=$2; next} $1 in m{$3=$5=m[$1]} 1 A.csv B.csv > C.csv

    2. </code>

  3. 2# 昵称为空呵呵 | 2019-08-31 10-32



    你可以尝试Perl




    1. perl -pe
      BEGIN { %kv=map{chomp;split(“,”)} qx(cat A.csv) }
      /^(.+?),/ and $kv{$1} and s/ABC_INT/ABC_STRING/g

    2. </code>


    使用给定的输入




    1. $ perl -pe BEGIN {%kv=map{chomp;split(“,”)} qx(cat A.csv)} /^(.+?),/ and $kv{$1} and s/ABC_INT/ABC_STRING/g B.csv
      TABLEA,SOMEVALUE,ABC_STRING,NULL,ABC_STRING
      TABLEB,SOMEVALUE,ABC_STRING,NULL,ABC_STRING
      TABLE1,SOMEVALUE,ABC_STRING,NULL,ABC_STRING
      TABLEC,SOMEVALUE,ABC_STRING,NULL,ABC_STRING
      TABLE2,SOMEVALUE,ABC_STRING,NULL,ABC_STRING
      TABLE3,SOMEVALUE,ABC_STRING,NULL,ABC_STRING

    2. $

    3. </code>

登录 后才能参与评论