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

2016-12-19 13:28:33 +08:00
 hailongs

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

2354 次点击
所在节点    程序员
7 条回复
hailongs
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
2016-12-19 13:29:49 +08:00
a_0.txt

abc

ddd

a_1.txt

xxx

xxxx

xxxxx

a_2.txt

1213
cute
2016-12-19 13:48:55 +08:00
sed 's/ #/\n/g' a.txt
hailongs
2016-12-19 14:04:13 +08:00
@cute 这个不行吧。。
slixurd
2016-12-19 14:09:42 +08:00
用 AWK 吧= =
awk '{if( $0~/^#/) x++ ;else print > "txt"x}' file
hailongs
2016-12-19 14:23:41 +08:00
@slixurd niuB,感谢!
tftk
2016-12-19 14:31:40 +08:00
echo $a | tr '#' '\n' | while read line;do echo $line > youfile.txt;done

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/328605

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX