Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-09-2008, 11:03 AM
|
#1
|
Senior Member
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305
Rep:
|
change desired field in passwd or shadow
Hi,
I need to change field as desired in /etc/passwd or /etc/shadow manually separated by delimiter ":"
For eg. I have an entry of /etc/shadow file
vikas:$1$WYD1mVMs$0ntLUvT4UgZ2FDa4V9spw:13991:0:99999:7:::
I need to change 2nd field of it manually which is between 2nd and 3rd colon , ":"
I did some awk and sed work but could suceed.
Pls help.
Thanks in adv.
VIKAS
|
|
|
06-09-2008, 11:28 AM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
In awk you can try something like this
Code:
awk -F: 'BEGIN{OFS=":"}{gsub(/.*/,"newstring",$3)}1' /etc/passwd
or if you want to selectively change the third field - e.g. based on the user name:
Code:
awk -F: 'BEGIN{OFS=":"}/vikas/{gsub(/.*/,"newstring",$3)}1' /etc/passwd
|
|
|
06-09-2008, 11:32 AM
|
#3
|
Senior Member
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
In awk you can try something like this
Code:
awk -F: 'BEGIN{OFS=":"}{gsub(/.*/,"newstring",$3)}1' /etc/passwd
or if you want to selectively change the third field - e.g. based on the user name:
Code:
awk -F: 'BEGIN{OFS=":"}/vikas/{gsub(/.*/,"newstring",$3)}1' /etc/passwd
|
Thank you so much, you people rock.
|
|
|
06-09-2008, 12:35 PM
|
#4
|
Senior Member
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305
Original Poster
Rep:
|
Hi Colucix,
Your code is working perfect BUT when I am passing variable in it, the desired position is not changing.
Like in this example, I am looking for user "vikas" in /etc/shadow
Code:
[root@RHEL vikas]# awk -F: 'BEGIN{OFS=":"}/vikas/{gsub(/.*/,"police",$2)}1' /etc/shadow
vikas:police:13991:0:99999:7:::
[root@RHEL vikas]#
[root@RHEL vikas]# a=$(cat file.txt | head -1 | awk '{print$1}')
[root@RHEL vikas]# echo $a
ZENITH
[root@RHEL vikas]# echo $b
vikas
I have tried passing the value of this variable a in this, but its not giving desired output.
I am looking for user vikas which is in variable b.
Code:
[root@RHEL vikas]# awk -F: 'BEGIN{OFS=":"}/$b/{gsub(/.*/,$a,$2)}1' /etc/shadow | grep vikas
Please help, guess I am making some small mistake.
Thanks in advance.
VIKAS
Last edited by vikas027; 06-09-2008 at 12:47 PM.
Reason: I had given a wrong entry
|
|
|
06-09-2008, 12:59 PM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
You cannot pass the value of a shell variable to an awk script in this way. Note that the awk code is enclosed in single quotes, hence $a is passed literally to awk (it is not expanded/evaluated by the shell). To pass an external variable to awk you have to use the -v option like this
Code:
awk -F: -v string=$a 'BEGIN{OFS=":"}/vikas/{gsub(/.*/,string,$2)}1' /etc/shadow
here you assign the value $a to the variable "string" and use this inside the awk code.
|
|
|
06-09-2008, 01:03 PM
|
#6
|
Senior Member
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305
Original Poster
Rep:
|
Hi,
I also meanwhile found other option
Quote:
[root@RHEL vikas]# awk -F: 'BEGIN{OFS=":"}/^'$b'/{gsub(/.*/,'$a',$2)}1' /etc/shadow | grep ^$b: | sed 's/'^$b':/&'$a'/1'
vikas:ZENITH:13991:0:99999:7:::
|
trying your option too.
Thanks mate, thanks a ton.
|
|
|
All times are GMT -5. The time now is 02:00 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|