Replace a specific column with a specific value

Hi,

I am looking to replacing value of a specific column of /etc/pam.d/system-auth file. My file looks like this

password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok 

expected result

password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok remeber=10 

Any suggestion would be appreciated...

Regards,
Litu

Hi Litu,

Following may help you in same.

echo "password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok" | awk '$(NF+1)="remeber=10"'
OR
awk '$(NF+1)="remeber=10"' filename
OR
echo "password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok" | awk '$(NF+1)=s1' s1="remeber=10"

Output will be as follows.

password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok remeber=101

Thanks,
R. Singh

this file may contain other entries as well. does this logic will work ?

also would you pls explain this command.

Litu,

This solution will work when you need to add variable at the last of the line. If any conditions are there for the variable to be added please let us know with full description, but if you need to add variable at last solution will work. Explaination of the solution is as follows.

echo "password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok" | awk '$(NF+1)="remeber=10" 1'

$(NF+1) means I am adding one more field at last of the line as a line will have acually NF number of fields only, then I am giving 1 which means I am making condition part set to true so be default print operation will take place. Please refer man awk also for same, will be helpful for you.

EDIT: Please use the following solution for same as above solution will not give requested output, thanks Franklin for pointing it out.

awk '$(NF+1)="remeber=10"' filename

Thanks,
R. Singh

awk '$(NF+1)="remember=10" 1'

doesn't give the correct output.

You should surround the command with braces:

awk '{$(NF+1)="remember=10"}1'

or

awk '$(NF+1)="remember=10"'

If you add a field with a value which is not empty, awk considers the action as true, so a 1 is superfluous.

Thanks Franklin52 for letting us know, I will remove that code but other code

echo "password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok" | awk '$(NF+1)=s1' s1="remeber=10"

is working fine.

Thanks,
R. Singh