Search and replace sed or tr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace sed or tr
# 1  
Old 10-24-2003
Network Search and replace sed or tr

Hi folks,
I need to search and replace specific text in a file and replace it. I have a text file that does not have any newlines or carriage returns. All newlines have been removed. Here is what I need to do.

Find the exact string “DH” (quotes included) and replace it with \n”DH” (basically putting a newline in front of all occurances of “DH”)

I have tried using sed...
sed 's/\<"DH"\>/\"DH"/g' DHt.txt > DH2.txt
When I tried the sed example the output has nothing in it. I think this is because there are no newlines or carriage returns.

And tr command...
tr -s '["DH"]' '[\\012"DH"]' < DHu.txt > DHt.txt
This replaces anything with DH in it and the newline is not added, it adds \012. It needs to to find only “DH”

Now I'm pretty new this type Unix commands so my syntax could easliy be wrong .

Thank all in advance,
Joaquin
# 2  
Old 10-27-2003
maybe it is hard to work only used sed or tr
but you cant do it both used sed and tr
for example :you can used sed to add a special char before "DH" then used tr to replace the special char by \n
# 3  
Old 10-27-2003
Hello Bridgeje,

One possibility is to open the file in vi and do the following:

:%s/"DH"/^M"DH"/g

for ^M you should type ctrl v, crtl enter
# 4  
Old 10-27-2003
I tired the insert character and earch and replace it with \n, but that speacial character could be in the file and I cannot replace it.

I also tried vi and then typing
:%s/"DH"/^M"DH"/g
But I must have done something wrong, when I type in the line it says "what" Is there a way to type it in the command line?

Thanlks for the help eveyone,
Joaquin
# 5  
Old 10-27-2003
maybe you can try this (i had ran the code in bash shell )
cat yourfile|sed s'/\(\"DH\"\)/\
> \1/'g
# 6  
Old 10-28-2003
The problem with these suggestions is that they are intended to be used on text files. The operative definition of text file is a collection of lines. The OP does not have a file that is a collection of lines nor would he have one if he succeeds in doing what he wants.

If I really had to solve this this problem, I would write a special purpose C program. I'm not a perl expert, but I strongly suspect that perl could also handle this problem.

Any perl programmers out there? Smilie
# 7  
Old 10-28-2003
Try using a sed script, containing...
Code:
s/DH/\
DH/g

Use it like this...
Code:
sed -f scriptfile file1 > file2


Last edited by Ygor; 10-28-2003 at 11:49 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace error using sed

Hi, Basically , i search for a string ( from variable) in a input file , if line found then search a string called "STRING" and replace with text (from another variable ). I cant avoid # delimiter due to esc sequences. #!/bin/bash LINE="source/path" REPLACE="TEXT/TO/REPLACE" sed -e... (9 Replies)
Discussion started by: greet_sed
9 Replies

2. Shell Programming and Scripting

Need help with search and replace using SED

Hi guys, thanks for accepting me in your forum .. I am trying to clean some hacked PHP files using SSH .. I am using this command: find . -type f -print0 | xargs -0 sed -i '/god_mod/d' <?php ... (3 Replies)
Discussion started by: wisam74us
3 Replies

3. UNIX for Dummies Questions & Answers

search and replace with sed

Hi All I have a simple text file and I want to be able to replace any alpha character and comma combination with any other symbol of my choice here is the text file I want to replace: pear apple ban,ana grape ",g1234," te,st1 here is how it should look afterwards: pear... (4 Replies)
Discussion started by: greg_b
4 Replies

4. Shell Programming and Scripting

Sed - search and replace help.

Hi everyone, basically I am been cleaning data by using simple sed commands So what i have below has been working for me. sed 's/="//g' trade.csv > tradeb.csv sed 's/"//g' tradeb.csv > trade2.csv but now i don't want to remove all the quotes just the ones if i encounter this ... (1 Reply)
Discussion started by: raz0r
1 Replies

5. UNIX for Dummies Questions & Answers

How to use 'sed' to search and replace?

Hello - I have a very large file in which a certain numbers are repeated. I find that using vi to edit the entire file is useless. How should i use sed to find a replace such as this text: To replace: 145.D25.D558 With: 215.22.45.DW I tried this command: sed... (4 Replies)
Discussion started by: DallasT
4 Replies

6. Shell Programming and Scripting

sed search and replace

hi, im new for sed, anyone can help me to these in sed command my output file.txt "aaa",a1,bbb "ddd",a1,ccc "eee",a1,www need to change a1, to "a1"," output i need "aaa","a1","bbb "ddd","a1","ccc "eee","a1","www thanks in advance fsp (2 Replies)
Discussion started by: fspalero
2 Replies

7. Shell Programming and Scripting

sed search and replace in next line

Hello, I am hoping someone can provide some guidance on using context based search and replace to search for a pattern and then do a search and replace in the line that follows it. For example, I have a file that looks like this: <bold>bold text </italic> somecontent morecontent... (3 Replies)
Discussion started by: charissaf67
3 Replies

8. UNIX for Dummies Questions & Answers

Search/Replace with Sed

Is there a way to use the sed command to 1) search a specified pattern 2) in the line where that pattern is found, replace from character N to character N+4 with a new 4-character string. Thks in advance! (5 Replies)
Discussion started by: mvalonso
5 Replies

9. UNIX for Dummies Questions & Answers

sed search and replace

Hello Folks, Anyone know how I can replace this line in file.xml <oacore_nprocs oa_var="s_oacore_nprocs">8</oacore_nprocs> with this line <oacore_nprocs oa_var="s_oacore_nprocs">1</oacore_nprocs> using sed or awk ? Thanks for your time. Cheers, Dave (7 Replies)
Discussion started by: d__browne
7 Replies

10. Shell Programming and Scripting

Help, sed search&replace

Plzzzz, tell me some script about this... What does this mean ? sed '/^ */s///' sed '/^/s// /' and why it's diferent ??? sed '/ */s// /g' and sed 's/ */ /g'. It's all the same ??? Thanks you very much (2 Replies)
Discussion started by: mle
2 Replies
Login or Register to Ask a Question