简体   繁体   中英

awk gensub function usage issue

cat file

^QciProfilePredefined=qci2$ logicalChannelGroupRef QciTable=default,LogicalChannelGroup=2
EUtranCellTDD=.*-1[123456],UeMeasControl=1,ReportConfigA4=1$ a4ThresholdRsrp -140

I want to use gensub() to delete the content before the last comma in $1(also delete the comma),without make influence other column which contains comma.

my code:

awk '{$1=gensub(/.*,/,"",1);print}' file

output:

LogicalChannelGroup=2 logicalChannelGroupRef QciTable=default,LogicalChannelGroup=2
ReportConfigA4=1$ a4ThresholdRsrp -140 a4ThresholdRsrp -140

It seems that the ROW 2 content repeated by "4ThresholdRsrp -140".

The output I expected:

LogicalChannelGroup=2 logicalChannelGroupRef QciTable=default,LogicalChannelGroup=2
ReportConfigA4=1$ a4ThresholdRsrp -140

gensub has 4 arguments gensub(regexp, replacement, how [, target])

you forgot to mention the target, default is $0 but you need $1

how argument is used to specify which match to be replaced, for ex: 2nd match or 4th match (like sed 's///3' ) and also accepts "g" or "G" to specify all matches

awk '{$1=gensub(/.*,/,"",1,$1);print}'


you don't need gensub here, sub will do inplace substitution for first match found. gsub will do inplace substitution for all matches found

awk '{sub(/.*,/,"",$1);print}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM