从表中提取特定数据
我有一个看起来像这样的表(制表符分隔):
Ron Rob rock bammy
m f m f
florida Atlanta florida texas
该表的大小为 5*512,基于第 3 行数据,我想提取 row1 中的值。 例如:我想要在一个 2 列 n 行的表中包含居住在佛罗里达州和德克萨斯州的所有人的姓名。
Florida Ron
Florida Rock
Texas BAmmy
等等。
关于 bash 或 PERL 衬垫的任何建议...
提前谢谢您。
I have table which looks like this (tab separated):
Ron Rob rock bammy
m f m f
florida Atlanta florida texas
This table is of order 5*512 and based on row 3 data, I want to extract the values in row1.
for example: I want to have names of all person living in florida and texas, in a table of 2 columns and n number of rows.
Florida Ron
Florida Rock
Texas BAmmy
and so on.
any suggestions for a bash or PERL liners...
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请参阅下面的测试:
输出
编辑
输出:
see the test below:
output
EDIT
output:
另一种 Perl 解决方案:
脚本,
或者作为循环内带有 if 条件的
而不是单独的 grep。我的方法是将所有三行展平为一个数组,并使用 for (0 .. $#data/3) 循环遍历与第一行中的名称相对应的索引并从中获取位置与
$data[$_+$#data/3*2+1]
匹配的列。Yet another Perl solution:
Or as a script
with an if condition inside the loop instead of the separate
grep
.My approach is to flatten all three lines into a single array and use
for (0 .. $#data/3)
to loop over the indexes corresponding to the names from the first line and get the location from the matching column with$data[$_+$#data/3*2+1]
.这是一个可行的 Perl 解决方案,但它比我想要的要复杂一些。您最好将这些数据放入数据库中。
Here's a Perl solution that works, but it's a bit more convoluted that I'd like. You'd probably be better off putting this data into a database.
在我看来,这是 Text::CSV_XS 的工作。正如许多人似乎建议的那样,在空白处进行分割不是一个好主意,因为除了简化数据之外,这对于任何其他东西都会失败。
代码:
输出:
Sounds to me like this is a job for Text::CSV_XS. It is not a good idea to split on whitespace, as many seem to be suggesting, as that will fail for anything but simplified data.
Code:
Output:
然后将其称为
“Oneliner”形式:
And then call it
"Oneliner" form: