无法在 AWK/Python 中将一个文件中的代码分离到多个文件中
我需要将不同的代码放在一个文件中到多个文件中。 该文件显然是由 AWK 的创建者在其主页上共享的。 该文件也位于此处,以方便使用。
我对问题的尝试
我可以获取每个代码所在的行
awk '{ print $1 }'
但是,我不知道如何
- 获取确切的行号,以便我可以使用它们
- 来收集特定行之间的代码,以便忽略每行的第一个单词,
- 将这些单独的代码放入新文件中,这些文件由该行的第一个单词命名。
我确信这个问题可以通过 AWK 和 Python 来解决。 也许,我们需要一起使用它们。
[编辑] 在第一个答案之后,
当我尝试使用 awk 执行它时,出现以下错误
$awk awkcode.txt
awk: syntax error at source line 1
context is
>>> awkcode <<< .txt
awk: bailing out at source line 1
I need to put different codes in one file to many files.
The file is apparantly shared by AWK's creators at their homepage.
The file is also here for easy use.
My attempt to the problem
I can get the lines where each code locate by
awk '{ print $1 }'
However, I do no know how
- to get the exact line numbers so that I can use them
- to collect codes between the specific lines so that the first word of each line is ignored
- to put these separate codes into new files which are named by the first word at the line
I am sure that the problem can be solved by AWK and with Python too. Perhaps, we need to use them together.
[edit] after the first answer
I get the following error when I try to execute it with awk
$awk awkcode.txt
awk: syntax error at source line 1
context is
>>> awkcode <<< .txt
awk: bailing out at source line 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试:
$1 != prev { close(prev); 上一页 = $1 }
{ print substr($0, index($0, " ") + 1) >$1 }
从文件 awkcode.txt 中删除以下行:
# unbundle - 将捆绑包解压到单独的文件
$1 != 上一个 { 关闭(上一个); 上一页 = $1 }
{ print substr($0, index($0, " ") + 1) >$1 }
awk -f unbundle.awk awkcode.txt
Did you try to:
$1 != prev { close(prev); prev = $1 }
{ print substr($0, index($0, " ") + 1) >$1 }
Remove the following lines form the file awkcode.txt:
# unbundle - unpack a bundle into separate files
$1 != prev { close(prev); prev = $1 }
{ print substr($0, index($0, " ") + 1) >$1 }
awk -f unbundle.awk awkcode.txt
awk 文件 awkcode.txt 不应包含任何空白行。 如果遇到任何空行,awk 程序就会失败。 没有错误检查来过滤代码中的空白行。 这是我经过几天的奋斗才发现的。
Awk file awkcode.txt should not contain ANY BLANK line. If any blank line is encountered, the awk program fails. There is no error check to filter out blank line in the code. This I could find out after several days of struggle.
您是否正在尝试解压该格式的文件? 这是一种 shell 存档。 有关详细信息,请参阅 http://en.wikipedia.org/wiki/Shar
如果您使用 awk 执行该程序,awk 将创建所有这些文件。 您不需要编写或重写太多内容。 您可以简单地运行该 awk 程序,它应该仍然可以工作。
首先,以“普通”格式查看文件。 http://dpaste.com/12282/plain/
其次,保存文件的纯文本版本as 'awkcode.shar'
第三,我认为您需要使用以下命令。
如果你想用Python程序替换它,那就是这样的。
Are you trying to unpack a file in that format? It's a kind of shell archive. For more information, see http://en.wikipedia.org/wiki/Shar
If you execute that program with awk, awk will create all those files. You don't need to write or rewrite much. You can simply run that awk program, and it should still work.
First, view the file in "plain" format. http://dpaste.com/12282/plain/
Second, save the plain version of the file as 'awkcode.shar'
Third, I think you need to use the following command.
If you want to replace it with a Python program, it would be something like this.