如何根据UNIX中所有拆分文件中从开头开始的行号匹配条件将文件拆分为多个文件?

如何根据UNIX中所有拆分文件中从开头开始的行号匹配条件将文件拆分为多个文件?,unix,awk,Unix,Awk,为此,我使用了下面使用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.t

为此,我使用了下面使用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

如果您需要打印计数虎钳(符合您的条件),请使用以下工具

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.

如果您需要打印计数虎钳(符合您的条件),请使用以下工具

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.
这里

结果

$ cat File1.txt 
1. LLLL
2. AAAA
$ 
$ cat File2.txt 
1. SSSS
2. DDDD
这里

结果

$ cat File1.txt 
1. LLLL
2. AAAA
$ 
$ cat File2.txt 
1. SSSS
2. DDDD

@oguzismail在这里,输入是words.txt。我提到了words.txt的内容。我想为每个“标题”数据创建新文件。@oguzismail这里,输入是words.txt。我提到了words.txt的内容。我想为每个“标题”数据创建新文件。感谢快速帮助。您可以创建一个文件,例如words.txt,并复制我在问题描述中提到的words.txt的内容。不管怎样,我已经测试了你的命令。它和我的一样。行号不会出现在从1开始的每个文件中。只需要下一个号码
1.LLLL 2.www3.EEEE
4.LLLL 5.www6.eeeeee操作不工作。我两个都试过了。同样的结果。不走运
1.lll2.www3.EEEE
4.lll5.ww6。EEEE@BhabadyutiBal,现在添加它以供参考。让我们也清理一下我们的评论,以获得清晰的帖子:)谢谢您的快速帮助。您可以创建一个文件,例如words.txt,并复制我在问题描述中提到的words.txt的内容。不管怎样,我已经测试了你的命令。它和我的一样。行号不会出现在从1开始的每个文件中。只需要下一个号码
1.LLLL 2.www3.EEEE
4.LLLL 5.www6.eeeeee操作不工作。我两个都试过了。同样的结果。不走运
1.lll2.www3.EEEE
4.lll5.ww6。EEEE@BhabadyutiBal,现在添加它以供参考。让我们也清理一下我们的评论,以获得清晰的帖子:)