Operating System - HP-UX
1827458 Members
5459 Online
109965 Solutions
New Discussion

sed syntax to insert carrage returns in middle of a line

 
SOLVED
Go to solution
Jaris Detroye
Frequent Advisor

sed syntax to insert carrage returns in middle of a line

I have a file in which the last line of a page & the first line of the next page are all one one line with a '^L' in the middle. (I don't have an ASCII cheat sheet to tell me what this control character is)
I would like to run this through sed to either replace the '^L' with a carrage return with the rest of the original line starting on a new line or add a carrage return in front of the cntrl-L.
Can any one suggest a pattern to do this?
15 REPLIES 15
James R. Ferguson
Acclaimed Contributor
Solution

Re: sed syntax to insert carrage returns in middle of a line

Hi:

Your reference is 'man 5 ascii'. To create the control characters use Ctrl-v and the character you see below:

# sed 's/^L/^M^L/g'

To exponse the non-printing characters, use:

# cat -etv

Regards!

...JRF...
Tom Maloy
Respected Contributor

Re: sed syntax to insert carrage returns in middle of a line

Ctrl-L is 014.

cat your_file_name | tr "\014" "\012"
Carpe diem!
Jaris Detroye
Frequent Advisor

Re: sed syntax to insert carrage returns in middle of a line

James --- U 'r da man !!!

You sir should collect all your postings and publish them as a reference book for the rest of us!!!

Jaris Detroye
Frequent Advisor

Re: sed syntax to insert carrage returns in middle of a line

My applogies... but James's fix does not work, but Tom's does for a simple replacement.

the line (sed 's/^L/^M^L/g') puts the characters "^M^" in front of any line beginning with a 'L' it does not deal with the control characters. It appears that the first ^ in the sed expression is interpreted as the anchor for a first character not as an cntrl-L character.

Tom's works but as it turns out I need to preserve the cntrl-L and add a cntrl-M before it.

Any more suggestions for sed ???
steven Burgess_2
Honored Contributor

Re: sed syntax to insert carrage returns in middle of a line

Hi Jaris

Here are a whole host of sed goodies

HTH

Steve
take your time and think things through
Tom Maloy
Respected Contributor

Re: sed syntax to insert carrage returns in middle of a line

You can just "escape" the first caret:

sed 's/^L/\^M^L/g'

Tom
Carpe diem!
Jaris Detroye
Frequent Advisor

Re: sed syntax to insert carrage returns in middle of a line

Thanks for the goodies, but I do not see one which will help in my problem.
If I could cause any text after a cntrl-L and including the cntrl-L to be put on a new line that would solve my problem.
James R. Ferguson
Acclaimed Contributor

Re: sed syntax to insert carrage returns in middle of a line

Hi (again):

See if this works for you (by example):

# echo "line^Lenil"|sed 's/\^L/\^J\^L/'g

NOTE that the caret (^) is produced by holding CTRL-v and typing the character seen following it.

Regards!

...JRF...
Jaris Detroye
Frequent Advisor

Re: sed syntax to insert carrage returns in middle of a line

That last one worked in as that it did correctly insert a '^M' in front of each occurence of a '^L'. But alas, my goal was not to have an additional control character in the line, but rather to force a new line beginning with the '^L' and what ever text followed on the origional line. Sorry I was not clear about that.
Robin Wakefield
Honored Contributor

Re: sed syntax to insert carrage returns in middle of a line

Hi Jaris,

but a newline IS a control character:

NEWLINE = Ctrl-J
CARRIAGE RETURN = Ctrl-M
FORM FEED (New Page) = Ctrl-L

Rgds, Robin
Jaris Detroye
Frequent Advisor

Re: sed syntax to insert carrage returns in middle of a line

I understand that, but when I run the sed string I get a control character I can see imbedded in the string. That is not what I need, I need it to physically begin a new line not show me the control character and continue on the same line.
Robin Wakefield
Honored Contributor

Re: sed syntax to insert carrage returns in middle of a line

Hi Jaris,

This is what I see:

# echo 'abc^Ldef' > /tmp/ff
# od -c /tmp/ff
0000000 a b c \f d e f \n
0000010
# cat /tmp/ff
abc
def (you may not see this but def is indented)
# sed 's/^L/^M^L/'g /tmp/ff
abc
def
# sed 's/^L/^M^L/'g /tmp/ff | od -c
0000000 a b c \r \f d e f \n
0000011

I only "see" the control characters if I forget to Ctrl-V first.

Rgds, Robin


James R. Ferguson
Acclaimed Contributor

Re: sed syntax to insert carrage returns in middle of a line

Hi (again) Jaris:

Then RETYPE this. Don't cut-and-paste. Rather, type CTRL-v following by the letter you see in lieu of the caret (^):

# echo "line^Lenil"|sed 's/\^L/\^J/'g
line

The sed expression will substitute a NEWLINE for a FORMFEED globally throughout the stream.

I think part of the confusion is in what constitutes a "newline" in Unix. Unix doesn't need a carriage-return with a linefeed like Microsoft.

Regards!

...JRF...
Jaris Detroye
Frequent Advisor

Re: sed syntax to insert carrage returns in middle of a line

The '^J' was the key. It works with this:
cat | sed 's/\^L/\^J\^L/g'

Thanks for all the help.... That Cntrl-V is a new one on me ....
James R. Ferguson
Acclaimed Contributor

Re: sed syntax to insert carrage returns in middle of a line

Hi Jaris:

Glad you have found what you needed. Robin's use of 'xd' is a valuable tool and worthy of noting.

BTW, don't waste a process with:

# cat |sed ...

Instead do:

# sed ...

Regards! :-)

...JRF...