为此,我使用了如下所示的awk命令
awk '/H.*/{x="F"++i;next}{print NR-1 "," $0 > x;}' words.txt
当任何“页眉”模式匹配时都会拆分为多个文件。
words.txt
Header
LLLL
AAAA
Header
SSSS
DDDD
现在拆分后通过上述命令获取输出
File1.txt
1. LLLL
2. AAAA
File2.txt
3. SSSS
4. DDDD
期望的是每个文件中从1开始的行号,如下所示
File1.txt
1. LLLL
2. AAAA
File2.txt
1. SSSS
2. DDDD
答案 0 :(得分:2)
如果需要打印台虎钳(与您的条件匹配),请使用以下内容。
awk '/H.*/{count=1;close(x);x="F"++i;next}{print count++ "," $0 > x;}' words.txt
还要添加close
以避免错误,这有时会给我们“打开的文件过多”
说明: 现在也添加了上述代码的说明。
awk ' ##Starting awk program here.
/H.*/{ ##Checking condition from H.* to till everything it covers in line.
count=1 ##Setting variable named count value to 1 here.
close(x) ##Closing the file(in case it is opened) whose value is variable x value. To avoid too many opened files error.
x="F"++i ##Creating variable x whose value is character F with increasing value of variable F each time with 1.
next ##next will skip all further statements.
}
{ ##Following statements will be executed when above condition is NOT TRUE.
print count++ "," $0 > x ##Printing variable count value with comma and current line value into file named x here.
}
' words.txt ##Mentioning Input_file name here.
答案 1 :(得分:1)
这里
awk '/^Header$/{close(f); f="File"++n".txt"; l=0; next}{print ++l". "$0 > f}' words.txt
结果
$ cat File1.txt
1. LLLL
2. AAAA
$
$ cat File2.txt
1. SSSS
2. DDDD