Issue
I'm trying to get the name of a variable between two different symbols in bash with the following command:
nfs4_getfacl .| grep -E ":.:.*:"
These are the strings obtained:
A:fdg:user@server:rxtTncC
A:g:user@server:xtcy
A:d:1111:xtcy
I'm trying to replace the value 1111 or any other numerical values that I come across in the list, this usually comes across as the third position according to the nfs4 permissions but is not always necessarily the case:
1111 -> replaced_value
A:fdg:user@server:rxtTncC
A:g:user@server:xtcy
A:d:replaced_value:xtcy
Solution
With your shown samples, please ty following. You could do this in a single awk
itself, we need not to use grep here. Set newValue
variable's value as per your need and it will replace value accordingly then.
nfs4_getfacl .|
awk -v newValue="newVALUE" 'BEGIN{FS=OFS=":"} /:.:.*:/ && $3~/^[0-9]+$/{$3=newValue} 1'
Explanation: Adding detailed explanation for above code.
awk -v newValue="newVALUE" ' ##Getting nfs4_getfacl output as input to awk program, creating newValue variable which has new value in it.
BEGIN{ FS=OFS=":" } ##Setting field separator and output field separator as : here.
/:.:.*:/ && $3~/^[0-9]+$/{ ##Check if line contains :.:.*: format AND 3rd column is digits.
$3=newValue ##Then set newValue value to 3rd column here.
}
1 ##printing edited/non-edited lines here.
'
Answered By - RavinderSingh13 Answer Checked By - David Goodson (WPSolving Volunteer)