Perl - 从特定行开始读取,只获取该行的第一列,直到结束
我有一个如下所示的文本文件:
Line 1
Line 2
Line 3
Line 4
Line 5
filename2.tif;Smpl/Pix & Bits/Smpl are missing.
有 5 行始终相同,第 6 行是我要开始读取数据的位置。读取数据时,每行(从第 6 行开始)均以分号分隔。我只需要获取每行的第一个条目(从第 6 行开始)。
例如:
Line 1
Line 2
Line 3
Line 4
Line 5
filename2.tif;Smpl/Pix & Bits/Smpl are missing.
filename4.tif;Smpl/Pix & Bits/Smpl are missing.
filename6.tif;Smpl/Pix & Bits/Smpl are missing.
filename8.tif;Smpl/Pix & Bits/Smpl are missing.
所需的输出将是:
filename2.tif
filename4.tif
filename6.tif
filename8.tif
这可能吗?如果可能,我从哪里开始?
I have a text file that looks like the following:
Line 1
Line 2
Line 3
Line 4
Line 5
filename2.tif;Smpl/Pix & Bits/Smpl are missing.
There are 5 lines that are always the same, and on the 6th line is where I want to start reading data. Upon reading data, each line (starting from line 6) is delimited by semicolons. I need to just get the first entry of each line (starting on line 6).
For example:
Line 1
Line 2
Line 3
Line 4
Line 5
filename2.tif;Smpl/Pix & Bits/Smpl are missing.
filename4.tif;Smpl/Pix & Bits/Smpl are missing.
filename6.tif;Smpl/Pix & Bits/Smpl are missing.
filename8.tif;Smpl/Pix & Bits/Smpl are missing.
Output desired would be:
filename2.tif
filename4.tif
filename6.tif
filename8.tif
Is this possible, and if so, where do I begin?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这使用 Perl 'autosplit'(或 'awk')模式:
请参阅 'perlrun' 和 ' perlvar'。
如果您需要在给定文件句柄和要跳过的行数的函数中执行此操作,那么您将不会使用 Perl 的“自动拆分”模式。
这会初始化一个循环,读取行,跳过前 N 个行,然后分割行并仅捕获第一个结果。
my($file) = split...
的那一行很微妙;括号意味着分割有一个列表上下文,因此它生成一个值列表(而不是值的计数)并将第一个值分配给变量。如果省略括号,您将为列表运算符提供标量上下文,因此您将获得分配给$file
的拆分输出中的字段数量 - 而不是您需要的数量。文件名被附加到数组的末尾,并返回该数组。由于代码没有打开文件句柄,因此不会关闭它。另一种接口会将文件名(而不是打开的文件句柄)传递到函数中。然后,您可以在函数中打开和关闭文件,并担心错误处理。如果您需要打开文件等方面的帮助,那么:
This uses the Perl 'autosplit' (or 'awk') mode:
See 'perlrun' and 'perlvar'.
If you need to do this in a function which is given a file handle and a number of lines to skip, then you won't be using the Perl 'autosplit' mode.
This initializes a loop, reads lines, skipping the first N of them, then splitting the line and capturing the first result only. That line with
my($file) = split...
is subtle; the parentheses mean that the split has a list context, so it generates a list of values (rather than a count of values) and assigns the first to the variable. If the parentheses were omitted, you would be providing a scalar context to a list operator, so you'd get the number of fields in the split output assigned to$file
- not what you needed. The file name is appended to the end of the array, and the array is returned. Since the code did not open the file handle, it does not close it. An alternative interface would pass the file name (instead of an open file handle) into the function. You'd then open and close the file in the function, worrying about error handling.And if you need the help with opening the file, etc, then:
有点难看,但是,读出虚拟线,然后分开;对于其余的人。
Kinda ugly but, read out the dummy lines and then split on ; for the rest of them.