sh - white Space in shell array -
i have issue when creating array from line separate "|_|", think whitespace creating problem here line contains fields containing white space
here in 1 line -
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|_| here have tried
#!/bin/sh # read subelement file while read line # 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
here perl 1 liner parses data:
cat subelement.dat | | perl -ne 'chomp;s/\cm//g;@f =split /\|_\|/, $_; $i=0;foreach (@f ) { print " ",++$i,"\t:=$_=:\n"};' perl script might help:
# open file open $input, "<", "subelement.dat" or die "unable open : $!"; # while loop read line of text @ time while(<$input>) { chomp; # remove end of line character s/\cm//g; # remove spurious control-m characters next if not length($_); # skip if line empty # parse whole line @fields = split /\|_\|/, $_; # parse line , place values in 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"; }
Comments
Post a Comment