Use gensub function
If you want to use it directly and only once, you can rely on gensub
:
awk -F= '$1=="User" {print gensub(/.*\//, "","g",FILENAME), $2;}' /opt/ops/resources/Int_Layer_Config.sh
Int_Layer_Config.sh svcOpsInt02
- second argument is "replace by nothing"
- third argument is for repeating the pattern. it could be 1 instead since .* is greedy, but "g" is a best practice on more restrictive patterns.
And you can still assign it if you prefer, readibility in mind:
awk -F= '$1=="User" {fname=gensub(/.*\//, "","g",FILENAME); print fname, $2;}' /opt/ops/resources/Int_Layer_Config.sh
Int_Layer_Config.sh svcOpsInt02
Below a more complex usage when the filename has a meaning in the data you process. I want the first part of the name, before _
awk -F= '$1=="User" {envname=gensub(/_.*$/,"","g",gensub(/.*\//, "","g",FILENAME)); print envname, $2;}' /opt/ops/resources/*_Layer_Config.sh
Int svcOpsInt02
Dev svcDev1
Qal svcDev3
NB: The same code works inside an awk script.