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 .
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
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?
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.