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

使用bat文件删除(x)列中的最后3行

  •  1
  • user8785018  · 技术社区  · 7 年前

    我有一个列中包含数据的文本文件。我想删除一列或多列中的x行数。 我的原始文件看起来:

    Column.1    Column.2    Column.3    Column.4    Column.5
    dhd21       393j        A/B/C/      1224        ouiyt
    nvd0        8ncbb       E/F/G       125a        b09bv
    xnxb        x9jd        m/f/e/      ljkjh       bhh66
    hyru        jsdj        M7D7W7Q7    mnuy        bbbb
    098y        sa56        by6         hhgt        xghsh
    

    我希望输出像这样, (例如,从第2列中删除了3行,从第4列中删除了4行) :

    Column.1    Column.2    Column.3    Column.4    Column.5
    dhd21       393j        A/B/C/      1224        ouiyt
    nvd0        8ncbb       E/F/G                   b09bv
    xnxb                    m/f/e/                  bhh66
    hyru                    M7D7W7Q7                bbbb
    098y                    by6                     xghsh
    

    我有代码来记录整个专栏:

    FOR /f "tokens=2,3 delims=  " %%B in (in.txt) do @echo %%B  %%C >> out.txt
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   michael_heath    7 年前

    这可以通过sqlite3批量完成。exe。如果没有sqlite3 现在,您可以从Sqlite获取它 download 页 它是一个小型的独立可执行文件。

    @echo off
    setlocal
    
    sqlite3 ^
    -cmd "create table t1 (c1, c2, c3, c4, c5)" ^
    -cmd ".mode tab" ^
    -cmd ".import test.csv t1" ^
    -cmd "update t1 set c4 = '' where c1 = 'nvd0'" ^
    -cmd "update t1 set c2 = '', c4 = '' where c1 = 'xnxb'" ^
    -cmd "update t1 set c2 = '', c4 = '' where c1 = 'hyru'" ^
    -cmd "update t1 set c2 = '', c4 = '' where c1 = '098y'" ^
    -cmd ".separator \"\t\" \"\r\n\"" ^
    -cmd ".once output.csv" ^
    -cmd "select * from t1" ^
    ":memory:" ""
    
    echo Done
    

    由于命令很长,因此 ^ 行尾是继续 下一行的命令。

    桌子 t1 在内存中创建,共有5列。 .mode tab .import test.csv t1 允许从文件导入操作 test.csv 使用制表符分隔符并插入到表中 t1级 .

    对表中的行进行了四次更新,其中 c1 = ( 要匹配的值 ). 如果满足条件,列2或4或两者都可以设置为空字符串。

    .separator ... 将制表符设置为分隔符,将CRLF设置为输出的行尾。 .once ... 将输出文件设置为 output.csv 对于下一个sql命令 只能处理一次。

    select * from t1 打印表的所有行和所有列 t1级 .

    :memory: 告诉sqlite3使用内存数据库而不是文件。 以下空引号只是一个空sql语句,因此 sqlite3将在没有交互的情况下关闭。

    注意:在双引号参数中使用双引号 \ 可以逃脱 内的双引号,即。 "\"abc\"" 被剥去外部 解释器使用引号,因此您将得到一个带引号的字符串 "abc" .