LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-09-2008, 10:03 AM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question 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
 
Old 06-09-2008, 10:28 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 06-09-2008, 10:32 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Smile

Quote:
Originally Posted by colucix View Post
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.
 
Old 06-09-2008, 11:35 AM   #4
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Question

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 11:47 AM. Reason: I had given a wrong entry
 
Old 06-09-2008, 11:59 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 06-09-2008, 12:03 PM   #6
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shadow file has !! for password field Z038 Linux - General 4 09-10-2007 07:40 AM
Shadow 'last change' field s0n|k Linux - Newbie 1 02-25-2006 11:45 AM
passwd shadow problem rblampain Linux - Distributions 2 10-04-2005 12:00 AM
/etc/shadow password field amfoster Linux - Security 2 08-24-2004 11:39 AM
/etc/passwd or /etc/shadow? tiger7007 Linux - Security 2 03-21-2004 04:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration