For a project I need to create a user registration with different types of users and functions, this user record is saved in a text file with the format:
Nombre:Contraseña:TipodeUsuario:cedula
This record is done correctly and my question is in the modification of this record which I do with a sed command that I do not understand exactly what the line does:
echo $(sed "${nLinea} s/:[^:]*/:${contrasena}/" usuarios.txt) > usuarios.txt
Try to read the sed manual to get the official information. For my part, I can explain to you that in:
We are going to decompose the command into several parts, keeping only what is inside the substitution command , that is:
From here, we can abstract the script
sed
like this:In the manual
sed
, in the sectionAddresses
, it is indicated that when a number is prepended, this part of the script will be executed only on the indicated line number.With the command
s
, on this line with the chosen number, the pattern will be searched,:[^:]*
, which will be taken by the regex enginesed
as "After a colon, find all the characters that are not the character ':' (colon )".After finding this pattern, it will be replaced by the string:
By not putting any address specification after the command
s/regexp/pattern/<flag>
, it will only be applied on the first match of this pattern. That is, if it finds a first colon, then only the substitution on the first match of the consequent dotless pattern will be applied. In other words, this substitution will be applied to the second field in a file of the form:Since "field1" does not take it into account because it does not have points, but ":field2" already takes it into account as the first match.
We can say that, if we have a file of the form:
To which we apply a
sed '2 s/:[^:]*/:nueva_contrasena/' archivo_prueba
, we will obtain the substitution in the second line:Instead, if we play with ,
sed '3 s/:[^:]*/:nueva_contrasena/2' archivo_prueba
, we now know thatsed
it will focus on the third line, and then look for the second match of the pattern "Colon, followed by several characters that are not periods", that is: