-1

I want to extract substring from following string after the Word "Device description" till Symbol "]". Substring length may vary. Is it possible to do it extract function.?

UC_CALLMANAGER [Device name abcde][Device IP address 1.1.1.1][Protocol SIP][Device type 550][Device description 9580 - Cordless][Reason Code 13][IPAddressAttributes 0][UNKNOWN_PARAMNAME:LastSignalRecei
4

4 回答 4

3

If the format of the string is going to same each time but the values only change you can try this

cat stck1.txt | awk '{print $12}'

where stck1.txt contains your string.

i have modified my command you can try this also which is more dynamic

cat stck1.txt | sed 's/.*\[Device description //' | awk '{print $1}'
于 2013-07-08T10:00:41.617 回答
2

这可行,但可能不是最理想的方式。

my $string = "UC_CALLMANAGER [Device name abcde][Device IP address 1.1.1.1][Protocol SIP][Device type 550][Device description 9580 - Cordless][Reason Code 13][IPAddressAttributes 0][UNKNOWN_PARAMNAME:LastSignalRecei";

my ($substr) = $string =~ /(?<=Device description )([^\]]+)/g;

print $substr;
于 2013-07-08T05:35:29.703 回答
1
perl -lne 'print $1 if(/Device description ([^\]]*)\]/)' your_file

在这里测试。想把它放在脚本中吗?

$_=~m/Device description ([^\]]*)\]/g;
my $info=$1;
# $info has the required informatio0n now!
于 2013-07-08T05:49:42.777 回答
0

这可能会有所帮助,并且参考链接示例的点击和试用将有助于您的目的,

#!/usr/bin/perl
  use strict;
  use warnings;

  # Find the location of the substring 'people'

  my $string = 'UC_CALLMANAGER [Device name abcde][Device IP address 1.1.1.1][Protocol SIP][Device type 550][Device description 9580 - Cordless][Reason Code 13][IPAddressAttributes 0][UNKNOWN_PARAMNAME:LastSignalRecei';
  my $fragment =  substr $string, index($string, 'Device description');

  my $limit = substr $fragment index($fragment, ']');

  print "  string: <$string>\n";
  print "fragment: <$fragment>\n";

  print " FINAL OUTPUT : <$limit>\n";

输出 :

字符串: UC_CALLMANAGER [设备名称 abcde][设备 IP 地址 1.1.1.1][协议 SIP][设备类型 550][设备描述 9580 - 无绳][原因代码 13][IPAddressAttributes 0][UNKNOWN_PARAMNAME:LastSignalRecei

片段:设备描述 9580 - Cordless][原因代码 13][IPAddressAttributes 0][UNKNOWN_PARAMNAME:LastSignalRecei

最终输出:设备描述 9580 - 无绳

有关详细信息,请查看:referenced with diff examples

于 2013-07-08T05:22:12.447 回答