Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Replace the perticular fileds in passwd file

Shell Programming and Scripting


Tags
awk, linux, sed, shell scripting

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by kumar85shiv
kumar85shiv's Unix or Linux Image
kumar85shiv kumar85shiv is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 20 November 2017, 8:23 AM EST
Posts: 36
Thanks: 5
Thanked 0 Times in 0 Posts
Linux Replace the perticular fileds in passwd file

Hi I have 2 different password entries in 2 different files for the same user.

file 1 -


Code:
siva:correct:1000:23:siva:/home/siva:/bin/bash

file 2 -


Code:
siva:incorrect:1000:23:siva:/home/siva:/bin/bash

file 1 is having correct passwd entry where as file 2 is wrong.

Now, i want to compare both lines and change passwd entry from file 1 to file 2

Finally file1 and file 2 should be the same ..


Code:
siva:correct:1000:23:siva:/home/siva:/bin/bash

Please suggest.

Last edited by Corona688; 4 Weeks Ago at 11:36 AM.. Reason: Code tags for code please.
Sponsored Links
    #2  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by kumar85shiv
RavinderSingh13's Unix or Linux Image
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 14 December 2017, 8:04 AM EST
Location: Chennai
Posts: 2,678
Thanks: 590
Thanked 1,275 Times in 1,146 Posts
Hello kumar85shiv,

Please use CODE TAGS as per forum rules for commands/codes/Input_file samples which you are using into your post.
Could you please try following and let me know if this helps you.

Code:
awk -F":" 'FNR==NR{a[$1]=$0;next} ($1 in a) && $2=="incorrect"{print a[$1];next} 1'  Input_file1   Input_file2

Above command will show output for only correcting Input_file2, if you are happy with above command's output then you could use following command to have output saved into Input_file2 itself.

Code:
awk -F":" 'FNR==NR{a[$1]=$0;next} ($1 in a) && $2=="incorrect"{print a[$1];next} 1'  Input_file1   Input_file2 > temp_file && mv temp_file  Input_file2

Thanks,
R. Singh
Sponsored Links
    #3  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by kumar85shiv
kumar85shiv's Unix or Linux Image
kumar85shiv kumar85shiv is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 20 November 2017, 8:23 AM EST
Posts: 36
Thanks: 5
Thanked 0 Times in 0 Posts
Thank you Ravindra, It works fine here
    #4  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by kumar85shiv
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 14 December 2017, 8:01 AM EST
Location: Aachen, Germany
Posts: 11,802
Thanks: 340
Thanked 3,659 Times in 3,357 Posts
It's highly dangerous to mess around with system files (e.g. password files) NOT using the tools designed for it. You might end up not being able to access the system.

For the sheer exercise: How is the user identified - by user name (field 1), UID (field 3), or both? And, it's just about field 2, so all the other fields in file 2 will stay the same? Assuming both, why don't you copy the entire line?
Try

Code:
awk -F: 'NR==FNR {T[$1,$3] = $0; next} $1,$3 in T {$0 = T[$1,$3]} 1' file1 file2

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to list todays file in perticular folder? pspriyanka UNIX for Beginners Questions & Answers 9 05-17-2016 08:11 AM
A script that read specific fileds from the 7th line in a file samura Shell Programming and Scripting 2 02-27-2009 06:26 AM
print the fileds of a file vijaya2006 Shell Programming and Scripting 3 01-30-2008 02:05 AM
Replace a perticular character of all lines of a file abovais Shell Programming and Scripting 1 12-11-2007 05:35 AM
Count the number of occurence of perticular word from file rinku Shell Programming and Scripting 40 08-10-2007 08:33 PM



All times are GMT -4. The time now is 09:04 AM.