To substitute a string in a line to another string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To substitute a string in a line to another string
# 1  
Old 10-31-2008
To substitute a string in a line to another string

Suppose,

d=ABC*.BGH.LKJ

Now I want to replace 'DEFGHIJ' instead of '*.B' and store the value in d. Any Idea? Can we use sed here?

The outout should be like this:

d=ABCDEFGHIJGH.LKJ

Please help..
# 2  
Old 10-31-2008
Code:
root@isau02:/data/tmp/testfeld> d="ABC*.BGH.LKJ"
root@isau02:/data/tmp/testfeld> d=`echo "${d}" | sed 's/\*\.B/DEFGHIJ/'`
root@isau02:/data/tmp/testfeld> echo "${d}"
ABCDEFGHIJGH.LKJ

# 3  
Old 10-31-2008
Quote:
Originally Posted by zaxxon
Code:
root@isau02:/data/tmp/testfeld> d="ABC*.BGH.LKJ"
root@isau02:/data/tmp/testfeld> d=`echo "${d}" | sed 's/\*\.B/DEFGHIJ/'`
root@isau02:/data/tmp/testfeld> echo "${d}"
ABCDEFGHIJGH.LKJ


SmilieThanks zaxxon
# 4  
Old 10-31-2008
Code:
echo "ABC*.BGH.LKJ" | sed 's/\*\.B/DEFGHIJ/'

# 5  
Old 10-31-2008
It's not necessary to escape the "." and the "*", this should be sufficient:

Code:
d=`echo "$d"|sed 's/*.B/DEFGHI/'`

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed]: Substitute a string with a multiline value

Dear all, I try to replace a string of characters in a file (MyFile.txt) by a multiline value of the variable "Myvar": $ cat MyFile.txt DESCRIPTION '@TargetTable SCHEMA' ( @InputFlowDef ); $ The content of Myvar: $ echo "$Myvar" col1 , col2 , col3 $ (4 Replies)
Discussion started by: dae
4 Replies

2. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

3. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

4. Shell Programming and Scripting

Grep for string and substitute if exits with a yes/no

Hi I have 3 files in total. file 1 is enriched.txt file2 is repressed.txt and file 3 is my content.txt What i need is query the content file against both enriched and repressed and wherever the gensymbol is same in both the files then add a yes value against it file1 Gene ABC XYZ MNO... (12 Replies)
Discussion started by: Diya123
12 Replies

5. Shell Programming and Scripting

Substitute string with an index number

Objective is to substitute Jan with 01, Feb with 02 and so on. The month will be provided as input. I could construct below awk and it worked. echo Jun | \ awk 'BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",mon," ")}{ for (i=1;i<=12;i++){ if ($1==mon) printf("%02d\n",i)} }' ... (4 Replies)
Discussion started by: krishmaths
4 Replies

6. UNIX for Dummies Questions & Answers

Find and substitute a string

Hi, I started exploring unix recently. Now i have got a requirement like i have a input file where i am having some strings line by line (One string Might be single line or multiple lines). Now i need find these strings in another file and if its found i have to replace it with another string... (2 Replies)
Discussion started by: Sivajee
2 Replies

7. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

8. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

9. Shell Programming and Scripting

Match keyword on string and substitute under vi

Hi guys, with sed when I need to make a substitution inside a line containing a specific keyword, I usually use: sed '/keyword/ s/cat/dog/g' This will substitute "cat" with "dog" on those lines containing "keyword". Now I want to use this inside vi, for several reason that I cannot... (2 Replies)
Discussion started by: lycaon
2 Replies

10. Shell Programming and Scripting

substitute string according line number

Hi all, I have an xml file which have several sections as the following: <process-type id="NIR" module-id="OC4J"> <module-data> <category id="start-parameters"> <data id="java-options" value="-server... (4 Replies)
Discussion started by: nir_s
4 Replies
Login or Register to Ask a Question