AIX / KSH从逗号分隔的行中提取字符串

我想从以下逗号分隔的行中提取部分“ virtual_eth_adapters”

lpar_io_pool_ids=none,max_virtual_slots=300,"virtual_serial_adapters=0/server/1/any//any/1,1/server/1/any//any/1","virtual_scsi_adapters=166/client/1/ibm/166/0,266/client/2/ibm/266/0",virtual_eth_adapters=116/0/263,proc_mode=shared,min_proc_units=0.5,desired_proc_units=2.0,max_proc_units=8.0

我在ksh中使用AIX。

我发现了awk和-F标志的变通方法,用分隔符分隔字符串,然后打印项目ID。但是,如果输入字符串发生更改,则ID可能会有所不同...

deronglpo 回答:AIX / KSH从逗号分隔的行中提取字符串

第一种解决方案: ,如果您也想在输出中打印字符串virtual_eth_adapters,请尝试以下操作。

awk '
match($0,/virtual_eth_adapters[^,]*/){
  print substr($0,RSTART,RLENGTH)
}
' Input_file

输出如下。

virtual_eth_adapters=116/0/263


第二种解决方案: :如果只想打印字符串virtual_eth_adapters的值,请尝试执行以下操作。

awk '
match($0,RSTART+21,RLENGTH-21)
}
' Input_file

输出如下。

116/0/263

说明: :添加代码说明。

awk '                                        ##Starting awk program here.
match($0,]*/){       ##Using match function of awk here,to match from string virtual_eth_adapters till first occurrence of comma(,)
  print substr($0,RLENGTH)            ##Printing sub-string whose starting value is RSTART and till value of RLENGTH,where RSTART and RLENGTH variables will set once a regex found by above line.
}
' Input_file                                 ##Mentioning Input_file name here.
,

我确实使用这些方法从中间提取数据。

awk -F'virtual_eth_adapters=' 'NF>1{split($2,a,",");print a[1]}' file
116/0/263

简短易学。 (无需计数或正则表达式)

  • -F'virtual_eth_adapters='virtual_eth_adapters=分隔行
  • NF>1,如果有多个字段(行包含virtual_eth_adapters=
  • split($2,")将行的最后部分分成由a分隔的数组,
  • print a[1]打印数组a的第一部分
,

还有另一种解决方案(假设字符串的位置)

awk -F\,'{print $7}'

如果仅需要该值,请尝试以下操作:

awk -F\,'{print $7}'|awk -F\= '{print $2}'

也可以通过这种方式获取值:

awk -F\,'{split($7,"=");print a[2]}'
本文链接:https://www.f2er.com/3037644.html

大家都在问