V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hailongs
V2EX  ›  程序员

shell 一个文本文件->多个怎么写?

  •  
  •   hailongs · 2016-12-19 13:28:33 +08:00 · 2337 次点击
    这是一个创建于 2690 天前的主题,其中的信息可能已经有所发展或是发生改变。

    a.txt 内容如下 #this is 0 context abc ddd #this is 1 context xxx xxxx xxxxx #this is 2 context 1213

    上面的 a.txt ,我想变成三个文件,靠#分割,变成 a_0.txt abc ddd

    a_1.txt xxx xxxx xxxxx

    a_2.txt 1213

    7 条回复    2016-12-19 14:31:40 +08:00
    hailongs
        1
    hailongs  
    OP
       2016-12-19 13:29:26 +08:00
    我去。。。

    #this is 0 context

    abc

    ddd

    #this is 1 context

    xxx

    xxxx

    xxxxx

    #this is 2 context

    1213
    hailongs
        2
    hailongs  
    OP
       2016-12-19 13:29:49 +08:00
    a_0.txt

    abc

    ddd

    a_1.txt

    xxx

    xxxx

    xxxxx

    a_2.txt

    1213
    cute
        3
    cute  
       2016-12-19 13:48:55 +08:00
    sed 's/ #/\n/g' a.txt
    hailongs
        4
    hailongs  
    OP
       2016-12-19 14:04:13 +08:00
    @cute 这个不行吧。。
    slixurd
        5
    slixurd  
       2016-12-19 14:09:42 +08:00
    用 AWK 吧= =
    awk '{if( $0~/^#/) x++ ;else print > "txt"x}' file
    hailongs
        6
    hailongs  
    OP
       2016-12-19 14:23:41 +08:00
    @slixurd niuB,感谢!
    tftk
        7
    tftk  
       2016-12-19 14:31:40 +08:00
    echo $a | tr '#' '\n' | while read line;do echo $line > youfile.txt;done
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2448 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 13:20 · PVG 21:20 · LAX 06:20 · JFK 09:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.