Replace a specific column with a specific value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a specific column with a specific value
# 1  
Old 09-01-2014
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

Code:
password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok

expected result

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

Any suggestion would be appreciated...

Regards,
Litu
# 2  
Old 09-01-2014
Hi Litu,

Following may help you in same.

Code:
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.

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


Thanks,
R. Singh

Last edited by RavinderSingh13; 09-01-2014 at 11:39 AM.. Reason: Added one more solution
# 3  
Old 09-01-2014
this file may contain other entries as well. does this logic will work ?

also would you pls explain this command.
# 4  
Old 09-01-2014
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.
Code:
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.

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

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-01-2014 at 11:44 AM.. Reason: Adding correct solution to the thread
# 5  
Old 09-01-2014
Quote:
Originally Posted by RavinderSingh13
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.
Code:
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.


Thanks,
R. Singh
Code:
awk '$(NF+1)="remember=10" 1'

doesn't give the correct output.

You should surround the command with braces:
Code:
awk '{$(NF+1)="remember=10"}1'

or
Code:
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.
# 6  
Old 09-01-2014
Thanks Franklin52 for letting us know, I will remove that code but other code
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific column delimiter

Hi All, I have a file with a pipe delimiter. I need to replace the delimiter with html tags. I managed to get all the delimiters replaced along with first and last but the requirement is that I need to change 7th delimiter with slight change. File1: ... (2 Replies)
Discussion started by: shash
2 Replies

2. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

3. Shell Programming and Scripting

Search Replace Specific Column using RegEx

Have Pipe Delimited File: > BRYAN BAKER|4/4/2015|518 VIRGINIA AVE|TEST > JOE BAXTER|3/30/2015|2233 MockingBird RD|ROW2On 3rd column where the address is located, I want to add a space after every numeric value - basically doing a "s//&\ / ": > BRYAN BAKER|4/4/2015|5 1 8 VIRGINIA AVE|TEST > JOE... (5 Replies)
Discussion started by: svn
5 Replies

4. Shell Programming and Scripting

How to replace a character in a specific column in a file?

This is a file that I have test line 1 (55) ) test line 2 (45) ) I would like to change all the parens in position 1 of this file to a ); i only want to check position 1 in every line of the file. I have tried different varations of sed, but cannot seem to be able to limit it to... (1 Reply)
Discussion started by: JoeG
1 Replies

5. Shell Programming and Scripting

Help with replace specific column command

Input file: ASD_QAW 12 A_@ AE_AQ 21 PA_123 ASDA_@ 23 ADA_AS . . Output file: ASD_QAW 12 A @ AE_AQ 21 PA 123 ASDA_@ 23 ADA AS . . Do anybody know how to just specific and replace "_" in column 3 with tab delimiter (\t)? Thanks for advice. (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. Shell Programming and Scripting

Bash Replace value in specific column

Hi all, I have two files with the following format: file1 BBB;33 AAA;2 CCC;5 file2 5;.;.;. 33;.;.;. The first file contain a list of code and numbers. The second file only the number. I would like to replace the corresponding code in the first column of the file1 with the... (3 Replies)
Discussion started by: g256
3 Replies

7. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

8. Shell Programming and Scripting

Help with replace column data with specific word

Input file: Populus_trichocarpa 30 0 50 0 0 US Vitis_vinifera 1 18 2 8 6 US Populus_trichocarpa 1 5 100 0 0 US Arabidopsis_lyrata_subsp._lyrata 0 90 0 0 0 US Glycine_max 0 2 3 0 70 UK Desired output file: Populus_trichocarpa YES NO YES NO NO US Vitis_vinifera YES YES YES YES YES US... (4 Replies)
Discussion started by: perl_beginner
4 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

10. Shell Programming and Scripting

How to replace a specific word in specific column?

Hi My orginal file is like (100s of lines) id host ip location remarks 1 host1 ip1 - xxx 2 host2 ip2 - xxx 3 host3 ip3 - xxx -- -- 9 host9 ip9 - xxx I have a ref file like host1 location1 host2 location2 host3 location3 -- --... (6 Replies)
Discussion started by: ./hari.sh
6 Replies
Login or Register to Ask a Question