如何将文本文件的内容操作到新的输出文件中

如何将文本文件的内容操作到新的输出文件中

我有一个如下所示的输入文本文件:

table columns are: 
number of vertices
total surface area (mm^2)
total gray matter volume (mm^3)
average cortical thickness +- standard deviation (mm)
integrated rectified mean curvature 
integrated rectified gaussian curvature
folding index
intrinsic curvature index
structure name

72 6.18 1307 87.23 987 0.566 2 3 1.8 SUBJECT_89765432/label/lh.APD57_20d.label

table columns are: 
....(repeat)

我想创建一个输出逗号分隔的变量文件,如下所示:

Id,surface area (mm^2),gray matter volume (mm^3),avg cortical thickness +- sd (mm),mean curvature,gaussian curvature,folding index,curvature index,hemi,ROI 
SUBJECT_89765432,72,6.18,1307,87.23,987,2,3,1.18, lh, 20d
SUBJECT_...(repeat)

我怎样才能做到这一点?非常感谢!

答案1

sed '/SUBJECT_/!d;s/ /,/g;s/\(.*\),\(SUBJECT_[0-9]*\).*/\2,\1/'
  • /SUBJECT_/!d删除所有不带关键字的行(无需通过脚本构建标头)
  • s/ /,/g用逗号代替空格
  • s/\(.*\),\(SUBJECT_[0-9]*\).*/\2,\1/重新排序

答案2

下面的脚本是对 perlText::CSV模块的一个非常hacky 的使用。通常,它用于解析并输出格式正确的 CSV 文件,但在这里我只是将它用于该say()方法,因为我不想编写自己的代码来在必要时正确引用字段(即当它们包含空格、行时) -feed、双引号或逗号)。

#!/usr/bin/perl -an
use Text::CSV qw(csv);

BEGIN {
  $csv=Text::CSV->new();
  # define our column headers in the right order.
  @columns = ("Id", "surface area (mm^2)", "gray matter volume (mm^3)",
    "avg cortical thickness +- sd (mm)", "mean curvature",
    "gaussian curvature", "folding index", "curvature index", "hemi", "ROI");
  # output them as a CSV field header.
  $csv->say(*STDOUT, \@columns );
};

# skip lines that don't begin with a digit
next unless /^\d/;

# Split the subject (field 10) into multiple sub-fields
# perl arrays start from 0, so the 10th field is $F[9].
# This will split it into an array like:
# SUBJECT 89765432 label lh APD57 20d label
#     0      1       2   3    4    5   6
my @subject=split(/[\/_.]/,$F[9]);

# Insert the first two of those sub-fields at the beginning of the input array
# as one new field joined by an underscore.
unshift @F, $subject[0] . "_" . $subject[1];

# Inserting that field means that field 10 is now field 11 - i.e. $F[9] is now $F[10].
# Replace it with the hemi value, and add a new ROI field at the end.
$F[10]=$subject[3];
push @F, $subject[5];

# print it as properly-formatted CSV.
$csv->say(*STDOUT, \@F);

输出:

Id,"surface area (mm^2)","gray matter volume (mm^3)","avg cortical thickness +- sd (mm)","mean curvature","gaussian curvature","folding index","curvature index",hemi,ROI
SUBJECT_89765432,72,6.18,1307,87.23,987,0.566,2,3,1.8,lh,20d

相关内容