awk分隔符设定为多个字符或字符串

awk -F"[01]" '{}'  这种形式指定的分隔符是或的关系,即0或1作为分隔符;
awk -F"[0][1]" '{}' 这种形式指定的分隔符是合并的关系,即以“01”作为一个字符为分隔符。

故假如有test.txt文本文件只有一行:

1. mail from: tomcat@gmail.com  2. subject:hello  3. data:2012-07-12 17:00  4. content:Hello, The world.  5.   6. mail from: jerry@gmail.com  7. subject:congregation  8. data:2012-07-12 08:31  9. content:Congregation to you.  10.   11. mail from: jacob@gmail.com  12. subject:Test  13. data:2012-07-12 10:20  14. content:This is a test mail.

用命令:awk  'BEGIN {FS="[0-9]+[.]";OFS=" "}{for(i=1;i<=NF;i++) printf("%d  %s ",i,$i)}' test.txt即可输出如下格式的形式的文件:

1  
2   mail from: tomcat@gmail.com  
3   subject:hello  
4   data:2012-07-12 17:00  
5   content:Hello, The world.  
6     
7   mail from: jerry@gmail.com  
8   subject:congregation  
9   data:2012-07-12 08:31  
10   content:Congregation to you.  
11     
12   mail from: jacob@gmail.com  
13   subject:Test  
14   data:2012-07-12 10:20  
15   content:This is a test mail.

原文地址:https://www.cnblogs.com/zejin2008/p/4685314.html