Hi,
I am having a user.txt contains the name of users and passwd.txt file contains as passwd.txt
$cat usr.txt
root
bin
daemon
cap
$cat passwd.txt
root:x:0:0:root:/root:/usr/bin/ksh
bin:x:1:1:bin:/bin:/sbin/csh
daemon:x:2:2:daemon:/sbin:/usr/bin/ksh
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/usr/bin/ksh
I want to change the 7th field of passwd.txt to /bin/sh when usr.txt users match with the 1st field of passwd.txt .
I have done so far ....
#!/bin/sh
file=/etc/log/user.txt
passwd_file=/var/log/passwd.txt
cat $file |while read i
do
awk -F: -v i="$i" '/$1 ~ i/{gusb("/usr/bin/ksh", "/bin/sh", $0);print > FILENAME} ' passwd_file
done
$cat passwd.txt
root:x:0:0:root:/root:/bin/sh
bin:x:1:1:bin:/bin:/sbin/csh
daemon:x:2:2:daemon:/sbin:/bin/sh
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/usr/bin/ksh
nawk 'FNR==NR{u[$1];next}$1 in u {$7="/bin/sh"}1' FS=: OFS=: usr.txt passwd.txt
or if you want to edit 'in-place':
nawk 'FNR==NR{u[$1];next}$1 in u {$7="/bin/sh"}{l[FNR]=$0}END {close(FILENAME);for(;++i in l;) print l>FILENAME}' FS=: OFS=: usr.txt passwd.txt
I think the first awk should be fine enough.
Thanks vgersh99 for the code
nawk 'FNR==NR{u[$1];next}$1 in u {$7="/bin/sh"}{l[FNR]=$0}END {close(FILENAME);for(;++i in l;) print l>FILENAME}' FS=: OFS=: usr.txt passwd.txt
But in my OS nawk is not supporting But if i go for awk this command work fine for me in SHELL But when i keep this thing in a shell script it won't works. Getting error unable to read passwd.txt file.
Thanks
Manabhanjan
manabhanjan:
Thanks vgersh99 for the code
nawk 'FNR==NR{u[$1];next}$1 in u {$7="/bin/sh"}{l[FNR]=$0}END {close(FILENAME);for(;++i in l;) print l>FILENAME}' FS=: OFS=: usr.txt passwd.txt
But in my OS nawk is not supporting But if i go for awk this command work fine for me in SHELL But when i keep this thing in a shell script it won't works. Getting error unable to read passwd.txt file.
Thanks
Manabhanjan
Well.... my crystal ball says that your script cannot find or read the passwd.txt file.
I'd suggest to start debugging the script.
Good luck.