代码之家  ›  专栏  ›  技术社区  ›  nightfox79

unix从文件中提取数据

  •  0
  • nightfox79  · 技术社区  · 7 年前

    我有一个 file.sql 从informix数据库导出的文件。

    grant dba to "xxx";
    grant dba to "yyy";
    grant dba to "zzz";
    
    { TABLE "xxx".table1 row size = 66 number of columns = 5 index size = 54 }
    { unload file name = table00100.unl number of rows = 307 }
    
    create table "xxx".table1 
      (
        field1 char(2) not null ,
        field2 char(10) not null ,
        field3 char(30),
        field4 char(20) not null ,
        field5 date not null 
      );
    revoke all on "xxx".table1 from "yyy";
    

    我需要从这个文件将table00100.unl文件命名为原始表名。所以我需要这样的输出:

    mv表00100.unl表1

    awk '{for(i=1;i<=NF;i++) {if ($i=="unload") {print $(i+4)} else {if ($i=="TABLE") print $(i+1)}}}' file.sql | sed 's/".*".//' > temp.out
    awk 'NR%2{printf "%s ",$0;next;}1' temp.out | awk '{for (i=NF;i>0;i--) if (i > 1) printf("mv %s ",$i); else printf("%s\n",$i)}' > temp.shl
    
    4 回复  |  直到 7 年前
        1
  •  4
  •   georgexsh    7 年前

    如果您想单独使用awk:

    /TABLE/ {
        sub("\".+\"\\.", "", $3);
        table = $3;
    }
    /unload/ {
        print "mv", $6, table;
    };
    
        2
  •  1
  •   steffen    7 年前

    类似于georgexsh的解决方案,但是 gensub

    awk '/TABLE/{table=gensub(/.*\./, "", "", $3)}/unload/{print "mv", $6, table }'
    
        3
  •  0
  •   kvantour    7 年前
    awk '($0 ~ /{ *TABLE/)  { match($0,/TABLE */); to=substr($0,RSTART+RLENGTH);
                              match(to," ");       to=substr(to,1,RSTART-1);
                              match(to,"[.]");     to=substr(to,RSTART+1);
                            }
         ($0 ~ /{ *unload/) { match($0,"name *= *"); from=substr($0,RSTART+RLENGTH); 
                              match(from," ");       from=substr(from,1,RSTART-1)
                            }
         (from!="") && (to!="") { exit }
         END {print "mv "from" "to}' file
    

    我之所以把它弄得如此“复杂”,是因为我不确定输入中的所有空格是否一致,大括号中的顺序是否总是相同的。

        4
  •  0
  •   stack0114106    7 年前

    使用一行perl代码(当然它很长:-)

    > cat informix_unload.txt
    grant dba to "xxx";
    grant dba to "yyy";
    grant dba to "zzz";
    
    { TABLE "xxx".table1 row size = 66 number of columns = 5 index size = 54 }
    { unload file name = table00100.unl number of rows = 307 }
    
    create table "xxx".table1
      (
        field1 char(2) not null ,
        field2 char(10) not null ,
        field3 char(30),
        field4 char(20) not null ,
        field5 date not null
      );
    revoke all on "xxx".table1 from "yyy";
    
    grant dba to "xxx";
    grant dba to "yyy";
    grant dba to "zzz";
    
    { TABLE "xxx".table2 row size = 66 number of columns = 5 index size = 54 }
    { unload file name = table00200.unl number of rows = 307 }
    
    create table "xxx".table2
      (
        field1 char(2) not null ,
        field2 char(10) not null ,
        field3 char(30),
        field4 char(20) not null ,
        field5 date not null
      );
    revoke all on "xxx".table1 from "yyy";
    -- other data
    
    > perl -ne 'BEGIN{$x=qx(cat informix_unload.txt);while($x=~m/(.+?)unload file name = (\S+)(.+?)create table (\S+)(.+)/osm){$x=$5;print "$2 $4\n";}exit}'
    table00100.unl "xxx".table1
    table00200.unl "xxx".table2
    >