我有一个以下格式的大文件:
From fc2868d745defcc4deb0ebdce8fe8dac78b447ea Mon Sep 17 00:00:00 2001
From: email address
message content
From 39a833c2f01030619629daa3b613e3985b2e4e7a Mon Sep 17 00:00:01 2001
From: email address
message content
From 833c2f01030619629daa3b613e3985b2e4e7a427 Mon Sep 17 00:00:01 2001
From: email address
message content
我想将文件拆分为多个文件,因此第一个文件包含
From fc2868d745defcc4deb0ebdce8fe8dac78b447ea Mon Sep 17 00:00:00 2001
From: email address
message content
下一个文件包含下一部分。
决定下一个文件的键是“From [a-z][0-9]”
所以“来自太空的一些字母/数字”
消息内容多行,最多1000行
有什么想法如何在 bash 中做到这一点吗?
包含 3 条消息的示例:http://pastebin.com/xWmuzfRC
最佳答案
#script 20151110f.sh
#input file: 20151110f.dat
#usage bash 20151110f.sh 20151110f.dat
inf=$1 # source file
num=1
while read -r line
do
echo "$line" | grep -q "From [a-z0-9]" # check if a new message
if [ $? -eq 0 ]
then
file="file"${num}.dat
echo "$line" > $file
num=$((num + 1)) #inc so that next file name will be one higher
else
echo "$line" >> $file
fi
done < "$inf"
关于regex - 将文件分成多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33636229/