how to search and replace string in column in file with command sed or other
search "INC0000003.in" and replace column 4 = "W"
$ cat file.txt
INC0000001.in|20150120|Y|N|N
INC0000002.in|20150120|Y|N|N
INC0000003.in|20150120|Y|N|N
INC0000004.in|20150120|Y|N|N
output
INC0000001.in|20150120|Y|N|N
INC0000002.in|20150120|Y|N|N
INC0000003.in|20150120|Y|W|N <---
INC0000004.in|20150120|Y|N|N
With awk:
awk -v key="INC0000003.in" -v val="W" '$1==key{$4=val}{print}' FS=\| OFS=\| file
Hardcoded version:
awk '$1=="INC0000003.in"{$4="W"}1' FS=\| OFS=\| file
sed version
sed '/^INC0000003\.in|/s/[^|]*|/W|/4' file
sed version with shell variables:
key='INC0000003\.in' val=W; sed "/^$key|/s/[^|]*|/$val|/4"
1 Like
if i want search "INC0000003.in" and replace last column = "W" .What command with sed
Aia
4
sed '/^INC0\{6\}3/s/.$/W/'
or
sed '/^INC0000003/s/.$/W/'
1 Like
That replaces the last character. To replace the last column in sed, try
sed '/^INC0000003\.in|/s/[^|]*$/W/' file