How do I combine 3 commands into 1 for regex and awk + want to regex on 3 possibilities e.g. 9,10,11

huangapple go评论64阅读模式
英文:

How do I combine 3 commands into 1 for regex and awk + want to regex on 3 possibilities e.g. 9,10,11

问题

将这3个命令合并成一个正则表达式命令,例如:

RRH[0_[9][10][11]_0]/SFP[0]

以下是合并后的命令及其输出:

awk -F, '$2=="AMWPF" && $4 ~ /^RRH\[0_(9|10|11)_0]\/SFP\[0]$/ {print $0}' data/20230706/IM_HW_20230706.csv

输出:

eNB_618378,AMWPF,RRHSFP,RRH[0_9_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211200053451  ,2021-03-19,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[9]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)
eNB_618378,AMWPF,RRHSFP,RRH[0_10_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211300012955  ,2021-03-26,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[10]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)
eNB_618378,AMWPF,RRHSFP,RRH[0_11_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211300012948  ,2021-03-26,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[11]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)
英文:

How do I combine these 3 commands

[senz_sims_db:/home/sims] awk -F, '$2=="AMWPF" && $4=="RRH[0_11_0]/SFP[0]" {print $0}' data/20230706/IM_HW_20230706.csv 
eNB_618378,AMWPF,RRHSFP,RRH[0_11_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211300012948  ,2021-03-26,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[11]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)
[senz_sims_db:/home/sims] 
[senz_sims_db:/home/sims] 
[senz_sims_db:/home/sims] 
[senz_sims_db:/home/sims] 
[senz_sims_db:/home/sims] 
[senz_sims_db:/home/sims] awk -F, '$2=="AMWPF" && $4=="RRH[0_10_0]/SFP[0]" {print $0}' data/20230706/IM_HW_20230706.csv 
eNB_618378,AMWPF,RRHSFP,RRH[0_10_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211300012955  ,2021-03-26,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[10]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)
[senz_sims_db:/home/sims] 
[senz_sims_db:/home/sims] 
[senz_sims_db:/home/sims] 
[senz_sims_db:/home/sims] 
[senz_sims_db:/home/sims] awk -F, '$2=="AMWPF" && $4=="RRH[0_9_0]/SFP[0]" {print $0}' data/20230706/IM_HW_20230706.csv 
eNB_618378,AMWPF,RRHSFP,RRH[0_9_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211200053451  ,2021-03-19,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[9]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)

into 1 regex command e.g. something like

xxxxRRH[0_[9][10][11]_0]/SFP[0]xxx

eNB_618378,AMWPF,RRHSFP,RRH[0_11_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211300012948  ,2021-03-26,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[11]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)
eNB_618378,AMWPF,RRHSFP,RRH[0_10_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211300012955  ,2021-03-26,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[10]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)
eNB_618378,AMWPF,RRHSFP,RRH[0_9_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211200053451  ,2021-03-19,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[9]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)

EDIT - based on answer below, 1st one does not work, 2nd does

>awk -F, '$2=="AMWPF" && $4~"^RRH\[0_(9|10|11)_0]/SFP\[0]$" {print $0}' data/20230706/IM_HW_20230706.csv 
awk: cmd. line:1: warning: escape sequence `\[' treated as plain `['
>awk -F, '$2=="AMWPF" && $4 ~ /^RRH\[0_(9|10|11)_0]\/SFP\[0]$/  {print $0}' data/20230706/IM_HW_20230706.csv 
eNB_618378,AMWPF,RRHSFP,RRH[0_9_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211200053451  ,2021-03-19,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[9]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)
eNB_618378,AMWPF,RRHSFP,RRH[0_10_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211300012955  ,2021-03-26,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[10]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)
eNB_618378,AMWPF,RRHSFP,RRH[0_11_0]/SFP[0],SFP,N/A,RTXM228-601     ,N/A,WTD             ,HN211300012948  ,2021-03-26,N/A,N/A,N/A,ENB[618378]/SHELF[0]/SLOT[1]/PORT[11]/CASC[0]/CPRI_PORT[0],N/A,-,wavelength=TX:1310(nm)/RX:-(nm)

答案1

得分: 1

$4 ~ /^RRH[0_(9|10|11)_0]/SFP[0]$/

$4 ~ "^RRH\[0_(9|10|11)_0]/SFP\[0]$"

$4 ~ "^RRH[[]0_(9|10|11)_0]/SFP[[]0]$"

请参阅 https://www.gnu.org/software/gawk/manual/gawk.html#Computed-Regexps 以了解两种分隔符 /" 之间的区别。

如果您更喜欢,也可以使用 (9|1[01]) 替代 (9|10|11)

英文:

Either of these will work:

$4 ~ /^RRH\[0_(9|10|11)_0]\/SFP\[0]$/

$4 ~ "^RRH\\[0_(9|10|11)_0]/SFP\\[0]$"

$4 ~ "^RRH[[]0_(9|10|11)_0]/SFP[[]0]$"

See https://www.gnu.org/software/gawk/manual/gawk.html#Computed-Regexps for the difference between the 2 delimiters, / vs ".

You could also use (9|1[01]) instead of (9|10|11) if you prefer.

答案2

得分: 0

你可以利用捕获组来提供可能的匹配。

作为注意,[] 字符是语法的一部分,所以我使用 . 作为通配符字符,而不是转义这些值。

RRH.0_(11|10|9)_0..SFP.0.

尽管你也可以使用以下方式:

RRH\[0_(11|10|9)_0\]\/SFP\[0\]

这是 AWK 文档,详细说明了正则表达式的使用。
Regexp (The GNU Awk User's Guide)

英文:

You can utilize the capture group, to provide the possible matches.

As a note, the [ and ] characters are part of the syntax, so instead of escaping the values, I am using a ., which is the wildcard character.

RRH.0_(11|10|9)_0..SFP.0.

Although, you could also use,

RRH\[0_(11|10|9)_0\]\/SFP\[0\]

Here is the AWK documentation, outlining regular expression use.
Regexp (The GNU Awk User’s Guide).

答案3

得分: 0

要么字符串样式:

    $4 ~ " ^RRH[[]0_(9|1[01])_0[]][/]SFP[[]0[]]$ "

或正则表达式样式

    $4 ~ /^RRH[[]0_(9|10|11)_0[]]\/SFP[[]0[]]$/
英文:

either string style :

$4 ~ "^RRH[[]0_(9|1[01])_0[]][/]SFP[[]0[]]$"

or regex style

$4 ~ /^RRH[[]0_(9|10|11)_0[]]\/SFP[[]0[]]$/

huangapple
  • 本文由 发表于 2023年7月6日 10:38:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定