如何使用拆分? (使用perl)

时间:2014-11-14 07:53:28

标签: perl

我的perl脚本有问题。我想首先解释一下脚本是如何工作的,这样你就可以了解如何修复它。 该脚本用于将数据从其他数据库获取到新数据库。对于示例我已将数据转换为纯文本然后我只需要获取新数据库的名称。这个过程我无法改变。

下面你会发现我已经为数据库取出查询var的脚本,因为否则你不会看到数据是如何出来的。

$match = "Name           | Primary Type | Sub Type    | Folder
---------------+--------------+-------------+----------------------
pc108220       | Device       | Workstation | /Devices/Workstations
PC114425       | Device       | Workstation | /Devices/Workstations
pc111206       | Device       | Workstation | /Devices/Workstations
pc111242       | Device       | Workstation | /Devices/Workstations
pc108947       | Device       | Workstation | /Devices/Workstations
pc108420       | Device       | Workstation | /Devices/Workstations
pc109364       | Device       | Workstation | /Devices/Workstations
pc106697       | Device       | Workstation | /Devices/Workstations
pc108844       | Device       | Workstation | /Devices/Workstations
PC106289VM_V32 | Device       | Workstation | /Devices/Workstations
pc108887       | Device       | Workstation | /Devices/Workstations
PC116786       | Device       | Workstation | /Devices/Workstations
pc106662-vme   | Device       | Workstation | /Devices/Workstations
pc108431       | Device       | Workstation | /Devices/Workstations
pc108873       | Device       | Workstation | /Devices/Workstations
pc105664       | Device       | Workstation | /Devices/Workstations
pc108872       | Device       | Workstation | /Devices/Workstations
pc109241       | Device       | Workstation | /Devices/Workstations
PC106662_VMA   | Device       | Workstation | /Devices/Workstations
pc109377       | Device       | Workstation | /Devices/Workstations
pc105372       | Device       | Workstation | /Devices/Workstations
pc108714       | Device       | Workstation | /Devices/Workstations
pc111244       | Device       | Workstation | /Devices/Workstations
pc115097       | Device       | Workstation | /Devices/Workstations
pc109379       | Device       | Workstation | /Devices/Workstations
pc107706-vm7   | Device       | Workstation | /Devices/Workstations
pc112170       | Device       | Workstation | /Devices/Workstations
pc109553       | Device       | Workstation | /Devices/Workstations
pc104597       | Device       | Workstation | /Devices/Workstations
pc106657       | Device       | Workstation | /Devices/Workstations
pc112183       | Device       | Workstation | /Devices/Workstations
pc109261       | Device       | Workstation | /Devices/Workstations
";

  if ($match =~ /pc[0-9][0-9][0-9][0-9][0-9][0-9]/) {
     @fields = split("Name           | Primary Type | Sub Type    | Folder| Device       | Workstation | /Devices/Workstations|", $match);
  $newline = join("", @fields);

  print $newline;
  }

else{
print 'no data found';
}

2 个答案:

答案 0 :(得分:3)

split的第一个参数是一个正则表达式,它应匹配输入中的某些内容。输入被分成围绕这些匹配的字段。

for my $line (split(/\n/, $match) {
  @fields = split(/\|/, $line);
}

将在循环内获取|分隔符之间的文本(您需要反斜杠,因为裸|是正则表达式字符)。您可以通过其他方式修剪值周围的空格。

  @fields = grep { s/^\s+|\s+$//g } split (/\|/, $match);

答案 1 :(得分:3)

local $/; $_ = <DATA>; $match=$_;
my @splitline = split/\n/, $match;
foreach my $Sngline(@splitline)
{
    print "Name: $&\n", if($Sngline=~m/^pc([^\s]*)\s/g)
}