shell数组中的空格

时间:2014-03-10 17:46:38

标签: arrays sh

当我从“| _ |”分隔的行创建数组时,我遇到了一个问题,我认为空白在这里会产生问题,因为行还包含一些包含空格的字段

这是一行 -

If<4>|_|10.127.101.49|_|10.127.101.49_If<4>|_|1393809196|_|If<4>|_|IF: 4 "100 Mbps" "Fa0/4 ergun-server4"|_|on|_|IETF_IF|_|ZerosReported|_|false|_|location|_|""|_|CounterSupport|_|32:32|_|AP_IfDescr|_|"FastEthernet0/4"|_|AP_ifStatus|_|up:up|_|ErrorsReported|_|true|_|TrafficDirection|_|both|_|DiscardsReported|_|true|_|AP_IfAlias|_|"ergun-server4"|_|ifConnectorPresent|_|true|_|MulticastSupport|_|NUcast|_|sysName|_|"utr-sw-06"|_|UsesHighSpeed|_|false|_|DisplaySpeed|_|"100 Mbps"|_|If|_|4|_|AP_ifType|_|ethernetCsmacd|_|AP_ifSpeed|_|100000000|_|

这是我试过的

#!/bin/sh

# Read the subelement file
while read line
do           

    # parse subelement record     
    fields=( ${line//\|_\|/ } )       

    # fixed fields
    instance=${fields[0]}
    eltName=${fields[1]}
    name=${fields[2]}
    date=${fields[3]}
    instance=${fields[4]}
    label=${fields[5]}
    state=${fields[6]}
    family=${fields[7]}
    eltDbIndex=${fields[8]}
    dbIndex=${fields[18]}
    missing=${fields[20]}  

done < /home/pvuser/gt/temp/subelement.dat

3 个答案:

答案 0 :(得分:0)

sh根本不支持数组。将shebang行更改为#!/bin/bash或其实际使用的shell。

答案 1 :(得分:0)

你可以试试awk,开始吧:

awk '{
      split($0,a,"[|]_[|]")
      name=a[1]
      eltName=a[2]
      print name,eltName
}' /home/pvuser/gt/temp/subelement.dat

如果字段中没有管道字符:

#!/usr/bin/bash
sed -e "s/|_|/|/g" /home/pvuser/gt/temp/subelement.dat |\
while IFS=\| read instance \
                  eltName \
                  name \
                  date 
do
    echo "${instance}"
    echo "${name}"
done

答案 2 :(得分:0)

这是一个解析数据的perl one liner:

cat subelement.dat |  | perl -ne 'chomp;s/\cM//g;@f =split /\|_\|/, $_; $i=0;foreach (@f ) { print "  ",++$i,"\t:=$_=:\n"};'

可能有帮助的perl脚本:

# open your file
open my $input, "<", "subelement.dat" or die "unable to open : $!";

# while loop that read a line of text at a time
while(<$input>)
{
    chomp;   # remove end of line character
    s/\cM//g;  # remove any spurious control-m characters
    next if not length($_);   # skip if the line is empty

    # parse the whole line
    my @fields = split /\|_\|/, $_;

    # parse the line and place the values in the variables
    my( $instance, $eltName, $name, $date,
        $instance, $label, $state, $family,
        $eltDbIndex, $dbIndex, $missing ) 
          = (split /\|_\|/, $_)[0, 1, 2, 3, 4, 5, 6, 7, 8, 18, 20];

    print "instance '$instance'\n";
    print "instance '$fields[0]'\n";
    print "\n";

    print "eltName  '$eltName'\n";
    print "eltName  '$fields[1]'\n";
    print "\n";

    print "name     '$name'\n";
    print "name     '$fields[2]'\n";
    print "\n";

    print "date     '$date'\n";
    print "instance '$instance'\n";
    print "label    '$label'\n";
    print "state    '$state'\n";
    print "family   '$family'\n";
    print "eltDbIndex '$eltDbIndex'\n";

    print "dbIndex    '$dbIndex'\n";
    print "dbIndex    '$fields[18]'\n";
    print "\n";

    print "missing    '$missing'\n";
}