How to search and replace string in column in file with command sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search and replace string in column in file with command sed?
# 1  
Old 08-25-2015
How to search and replace string in column in file with command sed?

how to search and replace string in column in file with command sed or other

search "INC0000003.in" and replace column 4 = "W"
Code:
$ 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
Code:
INC0000001.in|20150120|Y|N|N
INC0000002.in|20150120|Y|N|N
INC0000003.in|20150120|Y|W|N <--- 
INC0000004.in|20150120|Y|N|N


Last edited by Scrutinizer; 08-26-2015 at 12:35 AM.. Reason: CODE tags
# 2  
Old 08-26-2015
With awk:
Code:
awk -v key="INC0000003.in" -v val="W" '$1==key{$4=val}{print}' FS=\| OFS=\| file

Hardcoded version:
Code:
awk '$1=="INC0000003.in"{$4="W"}1' FS=\| OFS=\| file

sed version
Code:
sed '/^INC0000003\.in|/s/[^|]*|/W|/4' file

sed version with shell variables:
Code:
key='INC0000003\.in' val=W; sed "/^$key|/s/[^|]*|/$val|/4"


Last edited by Scrutinizer; 08-26-2015 at 01:34 AM.. Reason: Changed sed suggestion so that it also works if fields are empty
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-26-2015
Quote:
Originally Posted by Scrutinizer
With awk:
Code:
awk -v key="INC0000003.in" -v val="W" '$1==key{$4=val}{print}' FS=\| OFS=\| file

Hardcoded version:
Code:
awk '$1=="INC0000003.in"{$4="W"}1' FS=\| OFS=\| file

sed version
Code:
sed '/^INC0000003\.in|/s/[^|]*|/W|/4' file

sed version with shell variables:
Code:
key='INC0000003\.in' val=W; sed "/^$key|/s/[^|]*|/$val|/4"

if i want search "INC0000003.in" and replace last column = "W" .What command with sed
# 4  
Old 08-26-2015
Code:
sed '/^INC0\{6\}3/s/.$/W/'

or
Code:
sed '/^INC0000003/s/.$/W/'

This User Gave Thanks to Aia For This Post:
# 5  
Old 08-27-2015
That replaces the last character. To replace the last column in sed, try
Code:
sed '/^INC0000003\.in|/s/[^|]*$/W/' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

2. Shell Programming and Scripting

sed - search and replace whole string which contains dot

Hello. I would like to search exactly "string1.string2.string3" and replace it by "new_string1.new_string2.new_string3" And I would like to search exactly "string2.string3" and replace it by "new_string2.new_string3" And I would not found in the result : "string1.new_string2.new_string3"... (3 Replies)
Discussion started by: jcdole
3 Replies

3. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

6. Shell Programming and Scripting

How to use SED or AWK to search and replace an exact string

I have a file DS1 DDS DS I want to replace only "DS" to "DSmail.blah.com" in a lot of files. I tried sed 's/DS/DSmail.blah.com' but it changes all the lines . thanks in advance (2 Replies)
Discussion started by: gubbu
2 Replies

7. Shell Programming and Scripting

awk/sed string search and replace

Need help with either sed or awk to acheive the following file1 ----- In the amazon forest The bats eat all the time... mon tue wed they would eat berries In the tropical forest The bats eat all the time... on wed bats eat nuts In the rain forest The bats eat all the time... on... (2 Replies)
Discussion started by: jville
2 Replies

8. UNIX for Dummies Questions & Answers

Search and replace string only in a particular column in a delimited file

I have file with multiple columns. Column values for a record may be same. Now i have to replace a column value(this can be same for the other columns) with new value. File.txt A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D... (1 Reply)
Discussion started by: ksailesh
1 Replies

9. UNIX for Dummies Questions & Answers

How to search and replace a particular line in file with sed command

Hello, I have a file and in that, I want to search for a aprticular word and then replace another word in the same line with something else. Example: In file abc.txt, there is a line <host oa_var="s_hostname">test</host> I want to search with s_hostname text and then replace test with... (2 Replies)
Discussion started by: sshah1001
2 Replies

10. Shell Programming and Scripting

Need to replace all occurences of a search string using sed

All, Here is what I am searching for using sed. 1 00640000106798 I want to replace that with the following. 8 0064B0000106798 I can do this easy enough from the command line using sed but I need to put the search string in a file and then execute the sed command within a... (2 Replies)
Discussion started by: mjs3221
2 Replies
Login or Register to Ask a Question