我的档案如下
猫Data.txt
****************** abc.txt - Starting point ********************
abc
asd
asad
asfkn
sadjn
****************** abc.txt - End point ********************
****************** def.txt - Starting point ********************
fjhdsf
sdjnbs
askd
akdsn
dhskn
****************** def.txt - End point ********************
****************** xyz.txt - Starting point ********************
akdsnaskd
sakdnbasd
akjsndb
askjdb
akdsnb
****************** xyz.txt - End point ********************
我想根据起点和终点动态创建多个文件。
我需要的输出在上面的案例3文件中:abc.txt ,def.txt ,xyz.txt
>> cat abc.txt
abc
asd
asad
asfkn
sadjn
>> cat def.txt
fjhdsf
sdjnbs
askd
akdsn
dhskn
请你帮我一个unix命令来做这件事。
答案 0 :(得分:0)
这是一个AWK解决方案:
awk '/^(\*[ ]*)+/ {
gsub(/(\*[ ]*)+/,"")
match($0,/[ ]*-[ ]*Starting point/)
if(RLENGTH>0) fn=substr($0,1,RSTART-1)
next
}
fn { print $0 > fn }' Data.txt
假设“终点”和“起点”之间没有线。
结果:
$ cat abc.txt
abc
asd
asad
asfkn
sadjn
答案 1 :(得分:0)
这是一个易于阅读的解决方案Bash
和grep
:
#!/bin/bash
while read line ; do
if FILE=$(grep -P -o '[a-z]*\.txt(?= - Starting)' <<< "$line"); then
F="$FILE"
fi
if ! grep '\*\*\*\*' <<< "$line" ; then
echo "$line" >> "$F"
fi
done
它给出了以下结果
$ cat file.txt
****************** abc.txt - Starting point ********************
abc
asd
asad
asfkn
sadjn
****************** abc.txt - End point ********************
****************** def.txt - Starting point ********************
fjhdsf
sdjnbs
askd
akdsn
dhskn
****************** def.txt - End point ********************
****************** xyz.txt - Starting point ********************
akdsnaskd
sakdnbasd
akjsndb
askjdb
akdsnb
****************** xyz.txt - End point ********************
$ cat file.txt | ./go.sh
****************** abc.txt - Starting point ********************
****************** abc.txt - End point ********************
****************** def.txt - Starting point ********************
****************** def.txt - End point ********************
****************** xyz.txt - Starting point ********************
****************** xyz.txt - End point ********************
$ cat abc.txt
abc
asd
asad
asfkn
sadjn
$ cat def.txt
fjhdsf
sdjnbs
askd
akdsn
dhskn
$ cat xyz.txt
akdsnaskd
sakdnbasd
akjsndb
askjdb
akdsnb