Operating System - HP-UX
1833758 Members
2417 Online
110063 Solutions
New Discussion

New Line charactre in sed

 
SOLVED
Go to solution
Alex Galitsky
Occasional Contributor

New Line charactre in sed

Good day

I have a file with no New Lines characters, and would like to put New Line before certain pattern
Say, it looks like aa111bbccdd111eeerr111ttt
and 111 is indicator where new line should start

I tried to use command:


sed -e "s/111/`echo \\\t`111/g" NewFile
but it did not work :-(

Can you help me, please?

11 REPLIES 11
Charles McCary
Valued Contributor

Re: New Line charactre in sed

try:

sed 's/[pattern]/\n[pattern]/g'
Charles McCary
Valued Contributor

Re: New Line charactre in sed

sorry that won't work - don't know what I was thinking - hang on I'm looking.
Sachin Patel
Honored Contributor
Solution

Re: New Line charactre in sed

Hi Alex,
This will works

#echo "000111jkiejrn" > test

#sed 's/111/111X/g' test | tr "111X" "111\012]"
and result is what you want.
000111
jkiejrn


Sachin
Is photography a hobby or another way to spend $
S.K. Chan
Honored Contributor

Re: New Line charactre in sed

Not elegant but it works .. (2 steps needed..), assuming the file "test" has that line ..

# cat test|sed 's/111/:111/g'|tr ":" '\012'
harry d brown jr
Honored Contributor

Re: New Line charactre in sed

something like this:

#!/usr/bin/ksh
echo "aa111bbccdd111eeerr111ttt" |
awk '{X = split($0,A,/111/);
for (i=1; i {printf("%s\n",A[i]);}
}'



live free or die
harry
Live Free or Die
SHABU KHAN
Trusted Contributor

Re: New Line charactre in sed

Hi Alex,

I am not a sed expert but I tried :-)

hostAprompt>cat testfile
aa111bbccdd111eeerr111ttt

hostAprompt>sed 's/111/&new/' testfile | tr "new" "\n"
aa111
ewbbccdd111eeerr111ttt

close huh ?

-Shabu
SHABU KHAN
Trusted Contributor

Re: New Line charactre in sed

Oh yeah !

Do it globally ...(add g at the end)

sed 's/111/&new/g' testfile | tr "new" "\n"

-Shabu
Darrell Allen
Honored Contributor

Re: New Line charactre in sed

Hi Alex,

See my attached script. The sed command is split on 2 lines. The "\" at the end of the first line escapes the newline that terminates the line. Basically, it is simply continuing the command line on the next line.

It is important that there are no spaces before or after the "\".

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Joseph A Benaiah_1
Regular Advisor

Re: New Line charactre in sed

Alex,

Here is a variation on Harry's awk statement.

echo "aa111bbccdd111eeerr111ttt" |
awk -F"111" '{ for ( i = 0 ; i <= NF ; i++ )
print $i }'

This produces the following output:

aa
bbccdd
eeerr
ttt

Joseph.
H.Merijn Brand (procura
Honored Contributor

Re: New Line charactre in sed

l1:/tmp 107 > glob "aa111bbccdd111eeerr111ttt" > xx
l1:/tmp 108 > perl -lne 'BEGIN{$/="111"}print$_,$/' xx
aa111
bbccdd111
eeerr111
ttt111
l1:/tmp 109 >
Enjoy, Have FUN! H.Merijn
Alex Galitsky
Occasional Contributor

Re: New Line charactre in sed

To All:
thank you very much for your replays. I learned a lot :-)
I used Darrell Allen's approach - specail thanks to Darrell - but other's programs are also great!

Thank you again,

Alex