sed command to replace one value which occurs multiple times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to replace one value which occurs multiple times
# 1  
Old 04-11-2017
sed command to replace one value which occurs multiple times

Hi,

My Input File :
Code:
 "MN.1.2.1.2.14.1.1" := 
"MN_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 )
"MOS.1.2.1.2.13.6.2" := 
 "MOS_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 )

Like above template,I have multiple entries of MN and MOS.
I want to replace only "MOS_13_TM_4" value in the input file without touching "MN_13_TM_4" value,although both values are same i.e
Code:
000000110110100100110001111110110110101110101001100111110100011010110111001

using following command will replace the value at both places(MN & MOS)
Code:
i.e. sed 's/000000110110100100110001111110110110101110101001100111110100011010110111001/00000011011010010011000111111011011010111010100110011111010001101011011MMMM

How to replace only MOS value?
# 2  
Old 04-11-2017
Code:
sed 's/\(MOS_13_TM_4.*\)000000110110100100110001111110110110101110101001100111110100011010110111001/\100000011011010010011000111111011011010111010100110011111010001101011011MMMM/' file

# 3  
Old 04-11-2017
Try:

Code:
sed '/MOS_13_TM_4/s/\(00000011011010010011000111111011011010111010100110011111010001101011011\)1001/\1MMMM/' file

or

Code:
sed '/MOS_13_TM_4/s/\(\([01]\)\{71\}\)[01]\{4\}/\1MMMM/' file

or

Code:
sed '/MOS_13_TM_4/s/[01]\{4\} )$/MMMM )/' file


Last edited by Scrutinizer; 04-11-2017 at 02:17 PM..
# 4  
Old 04-12-2017
@balajesuri

if I defined following variables -
Code:
set x=MOS_13_TM_4
set value=000000110110100100110001111110110110101110101001100111110100011010110111001
set tmp=100000011011010010011000111111011011010111010100110011111010001101011011MMMM

Can you correct the following command ? :
Code:
	sed -i	's/\('$x'.*\)'$value'/\'$tmp'/' file

# 5  
Old 04-12-2017
You could change it to:
Code:
	sed -i	's/\('$x'.*\)'$value'/\1'$tmp'/' file

and it will work with your sample data, but
Code:
	sed -i	"s/\($x.*\)$value/\1$tmp/" file

is MUCH easier to read and will still work if either or both of your search pattern and replacement string contain spaces and/or tabs.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace multiple column in one go

Hi, I want to replace the value in more than one column. For one column ,following command is working - sed 's/./$value_to_replace/$column number' file_name e.g. suppose this is input 1111000000 command - sed 's/./M/5' output= 1111M000000 For two column also command is like - cat... (22 Replies)
Discussion started by: Preeti Chandra
22 Replies

2. Shell Programming and Scripting

Counting number of times content in columns occurs in other files

I need to figure out how many times a location (columns 1 and 2) is present within a group of files. I figured using a combination of 'while read' and 'grep' I could count the number of instances but its not working for me. cat file.txt | while read line do grep $line *08-new.txt | wc -l... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

3. UNIX for Dummies Questions & Answers

Replace string and create new file multiple times

First of all, apologies if this has already been answered elsewhere. I haven't quite been able to find what I'm looking for yet, so hopefully this won't come across as repetition. I have a file consisting of ~100 nearly identical lines, each of which contains multiple instances of the string I... (11 Replies)
Discussion started by: pseudo.seppuku
11 Replies

4. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

5. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

6. UNIX for Dummies Questions & Answers

how to read how many times same result occurs?

Is there a way to read how many times each result occurs in a list, say the list.txt has the results: a c d a f c a d e and I need to know how many times each of them occurs such as : a=3 c=2 ..etc. (5 Replies)
Discussion started by: Iifa
5 Replies

7. Shell Programming and Scripting

Removal of a tag in the xml which occurs mutiple times

Hi, The tag can occur multiple times. I want to remove entire <SeqNum>..</SeqNum> from each line, regardless the values with in this tag. for each line value inside these tags could be different. So please suggest how to do this. Note: Each profile information is in one line. ... (2 Replies)
Discussion started by: Anusha_Reddy
2 Replies

8. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies

9. Shell Programming and Scripting

TO find the word which occurs maximum number of times

Hi Folks !!!!!!!!!!!!!!!!!!! My Requirement is............. i have a input file: 501,501.chan 502,502.anand 503,503.biji 504,504.raja 505,505.chan 506,506.anand 507,507.chan and my o/p should be chan->3 i.e. the word which occurs maximum number of times in a file should be... (5 Replies)
Discussion started by: aajan
5 Replies

10. Shell Programming and Scripting

using sed command to replace multiple lines

the file contains the follwoing lines /* * Copyright (C) 1995-1996 by XXX Corporation. This program * contains proprietary and confidential information. All rights reserved * except as may be permitted by prior written consent. * * $Id: xxx_err.h,v 1.10 2001/07/26 18:48:34 zzzz $ ... (1 Reply)
Discussion started by: radha.kalivar
1 Replies
Login or Register to Ask a Question