Replace the perticular fileds in passwd file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace the perticular fileds in passwd file
# 1  
Old 11-14-2017
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; 11-14-2017 at 11:36 AM.. Reason: Code tags for code please.
# 2  
Old 11-14-2017
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
# 3  
Old 11-14-2017
Thank you Ravindra, It works fine here
# 4  
Old 11-14-2017
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to list todays file in perticular folder?

How to list todays file in perticular folder Moved thread to appropriate forum (9 Replies)
Discussion started by: pspriyanka
9 Replies

2. Shell Programming and Scripting

How to selectively NOT output some fileds?

Dear All I have a text file which has many columns (>10,000). I want to create a new text file which will NOT include following columns: 5,15,105,200. How can I do that in shell (or awk, perl)? Thanks. (6 Replies)
Discussion started by: littlewenwen
6 Replies

3. Shell Programming and Scripting

Re: using AWK to compare fileds

I have following text: NAME=ora.LISTENER.lsnr TYPE=ora.listener.type TARGET=ONLINE , ONLINE , ONLINE , ONLINE STATE=ONLINE on host1, ONLINE on host2, ONLINE on host3, ONLINE on host4 NAME=ora.LISTENER_1525.lsnr TYPE=ora.listener.type TARGET=ONLINE ... (2 Replies)
Discussion started by: rcc50886
2 Replies

4. AIX

When did AIX start using /etc/security/passwd instead of /etc/passwd to store encrypted passwords?

Does anyone know when AIX started using /etc/security/passwd instead of /etc/passwd to store encrypted passwords? (1 Reply)
Discussion started by: Anne Neville
1 Replies

5. Shell Programming and Scripting

A script that read specific fileds from the 7th line in a file

Dear All, I need a unix script that will read the 7th line and especially these fileds from a file Mo speed 16, Mt speed 15 every 15 minutes starting from 00:00 to 23:45 on daily basis and put the result in a txt file and name it MT_MO_20090225.txt, please also note that the system date format... (2 Replies)
Discussion started by: samura
2 Replies

6. Shell Programming and Scripting

print the fileds of a file

Hi Friends, Please help me in finding the solution for this : I have a file as below : CuDv: name = "hdisk0" status = 1 chgstatus = 2 ddins = "scsidisk" location = "" parent = "vscsi0" connwhere = "830000000000" PdDvLn... (3 Replies)
Discussion started by: vijaya2006
3 Replies

7. Shell Programming and Scripting

Replace a perticular character of all lines of a file

Hi all, I am new to UNIX, so sorry if my question seem stupid to u. well i want to replace the first character of first 30 lines of a file, only if the first character is h. and in anothe script i want to replace a particular string/character say hello/h of a file.Condition: It should... (1 Reply)
Discussion started by: abovais
1 Replies

8. UNIX for Advanced & Expert Users

Mandatory fileds in Bugzilla

Hi, We need to configure some of the fileds in bugzilla like Platform,OS, Version etc are mandatory. Is it possible to set, if yes then how to configure. Thanks & Regards, Bache (0 Replies)
Discussion started by: bache_gowda
0 Replies

9. Shell Programming and Scripting

Count the number of occurence of perticular word from file

I want to count the number of occurence of perticular word from one text file. Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work. :confused: (40 Replies)
Discussion started by: rinku
40 Replies

10. Shell Programming and Scripting

Merging fileds from 2 files

I have 2 files - fileA and fileB I need to match the first field of the 2 files then output some of the fields from fileA on the same line as certain fields from fileB. There will be instances where fileB does not have a match for first field in fileA Thanks in advance (8 Replies)
Discussion started by: Mudshark
8 Replies
Login or Register to Ask a Question