简体   繁体   English

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

[英]how to use split? (using perl)

I have a problem with my perl script . 我的perl脚本有问题。 I would like to explain first how the script works so that you get an idea of how or what you can fix. 我想先解释一下脚本是如何工作的,这样您就可以知道如何或可以修复什么。 the script is used to get data from an other database to an new one . 该脚本用于将数据从另一个数据库获取到新数据库。 for the example i have taken al the data into plain text en then i need to only take the names for the new database. 对于该示例,我已经将所有数据都转换为纯文本格式,那么我只需要使用新数据库的名称。 This proces i can't change. 这个过程我无法改变。

below you will find the script i have taken the query var for the database out because otherwise you wont see how the data is coming out . 在下面,您将找到我已将数据库查询变量带出的脚本,因为否则您将看不到数据的输出方式。

$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';
}

The first argument to split is a regular expression which should match something in the input. split的第一个参数是一个正则表达式,应与输入中的内容匹配。 The input is split into fields around those matches. 输入将围绕这些匹配项拆分为多个字段。

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

would obtain the text between the | 将获取|之间的文本 separators (you need to backslash it because a bare | is a regex metacharacter) inside the loop. 循环内的分隔符(您需要反斜杠,因为裸露的|是正则表达式元字符)。 You can trim the spaces around the values by other means. 您可以通过其他方式修剪值周围的空格。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM