Hi, guys. I have one question:
How can I search for a line with certain string in it and then insert a string into this line?
For example:
There is a file called shadow, the contents of it are below:
**************************
...
yuanz:VIRADxMsadfDF/Q:0:0:50:7:::
sherrys:ASdfe#$DfdadfFAd:0:0:50:7:::
brownj:ADdfTRMnD$%3dfa:0:0:50:7:::
...
**************************
A variable called $user, another variable called $string. I want to search the line which has $user in it, and add $string at the beginning of the second column. If $user is brownj, and $string is STRING_, then the line will be:
brownj:STRING_ADdfTRMnD$%3dfa:0:0:50:7:::
I use the following code, but it doesn't work
sed 's/^$user:/$user:$string/' shadow
Can anybody help me with this?
Thank you very much for your time in advance
-Keyang
it is advisable not to change your shadow file like that. (however its still possible). change the password using commands such as passwd, usermod ...
hi, ghostdog74.
I am not changing the password in this way, I want to suspend the user, I add a string to the encrypted password, so the user is not able to login. I am new to Linux, so I do not really know whether or not this is a good practice. And as a practice, I could not user command like "passwd" and "usermod". Still, thanks for your reply.
@general :
Does anybody can help me with this question?
Thank you very much for your time in advance
-Keyang
please check the passwd and usermod man page. there are options to "suspend" user.
awk again
awk '{FS=OFS=":"}/brownj/{$2="STRING_"$2}1' shadow
Hi, ghostdog74.
I am not supposed to use commands like suspend and passwd for this exercise.
Still, thanks for the reply.
-Keyang
Hi, Dan.
Thanks for answering again! I will try it out.
But can you tell me what is wrong with my code? if I don't use variables in my code, it works well. For example:
sed 's/^brownj:/brownj:STRING_/' shadow
It will find brownj and replace it with brownj:STRING_.
Thanks for your time in advance
-Keyang
Hi, Dan.
The code doesn't work for my case, the same problem I met before with my code. It seems that I cannot pass any variable to awk and sed.
@general :
Is there anybody know how to solve this problem?
Thanks for your time in advance
-Keyang
daikeyang:
Hi, Dan.
The code doesn't work for my case, the same problem I met before with my code. It seems that I cannot pass any variable to awk and sed.
@general :
Is there anybody know how to solve this problem?
Thanks for your time in advance
-Keyang
sed "s/^$user:/$user:$string/" shadow
awk 'BEGIN {FS=OFS=":"}$1 == usr {$2=str}1' usr="${user}" str="${string}" shadow
Hi, Vgersh99.
Thank you very much for your reply!
-Keyang