Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SED - Search for a letter replace with a carriage return

Status
Not open for further replies.

keusch

Technical User
Jun 17, 2001
41
US
I have a script that reads a file and one of 20 steps should search for a "D" and replace the "D" with a carriage return. What would be the command to do this subsitution.
Right now I'm getting to this point and take the file into VI run a macro then start the next script. Any suggestions to do it in one step?
input
17597947.53 1954112.92D 17597947.53 1954098.27D 17597496.95 1953010.46D 17596664.38 1952177.89D 17595576.57 1951727.31D 17594399.15 1951727.31
17593311.34 1952177.89D 17592478.77 1953010.46D 17592028.19 1954098.27D 17592028.19 1955275.69D 17592478.77 1956363.50D

desired output
17597947.53 1954112.92
17597947.53 1954098.27
17597496.95 1953010.46
17596664.38 1952177.89
17595576.57 1951727.31
17594399.15 1951727.31
17593311.34 1952177.89
17592478.77 1953010.46
17592028.19 1954098.27
17592028.19 1955275.69
17592478.77 1956363.50

Thanks :)
 
tr 'D' '\n' < file.txt vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
a bit better:
tr 'D' '\n' < file.txt | sed 's/^ *//g'


and after all it's an AWK forum:
nawk 'BEGIN {RS=&quot;D&quot;} {gsub(&quot;^ *&quot;, &quot;&quot;);print}' file.txt vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
This part of the script reads:
sed '
{
s/D/\n/g
}
' 1.txt > 2.txt
which does not work. Is that what you were suggesting??
Please advise.
 
OK - the 1st suggestion work just fine - I was just a tad braindead.
Thanks for the info!!

 
for sed try out

sed '
s/D//g
' 1.txt > 2.txt

or if you don't use the hold space you could do something wierd, slow, but occasionally handy -

sed '
/D/{
G
:a
s/\(.*\)D\(.*\)\(\n\)/\1\3\2\3/
t a
s/\n$//
}
' 1.txt > 2.txt

The /D/ is there to skip lines that don't need to be processed. The :a label with t starts recurses the line until all D's are replaced with new lines. The last substitution removes the extraneous newline.

P.S. when you say carriage return I assume you mean a new line or 0A-line feed character not 0D-carriage return in the DOS sense. Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
bigoldbulldog,
regarding your P.S., yes carriage return = new line (like a typewriter or perhap's that too dated for most users tuning in)
As for yur second suggestion - that's certainly a learning exercize. I'll try to digest that one.
Thanks for the tip!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top