Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep specific variable from output and replace value with another linux

Tags:

grep

shell

awk

nfs

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
like image 929
Joe_Informatics Avatar asked Jul 11 '26 22:07

Joe_Informatics


1 Answers

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.
'
like image 112
RavinderSingh13 Avatar answered Jul 14 '26 14:07

RavinderSingh13