sed to replace <cr><cr><cr> in a line with <cr>

Hi,
Please help me with the sed command for the following scenario:

I've in abc.txt,

asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr>

I want a sed command to do

asdasdada<cr>aasdsad<cr>asdasdfsdfs<cr>asdsada<cr>adasd<cr>daasdaasd<cr>

please help me

Assuming your file does not contain any '@'
otherwise, choose another character that does not already appear in your file (#,�, ...)

sed 's/<cr>/@/g;s/@@*/@/g;s/@/<cr>/g' infile

something like this:

 
sed 's/<cr>[<cr>]*/<cr>/g' input_file

Thanks for the reply.
If possible, could you please let me know what is wrong with

sed "s/<cr>+/<cr>/g" infile.txt

or

sed "s/\(<cr>\)+/<cr>/g" infile.txt

Thanks in advance.

Please go through this link. I'm sure it will better explain than me:

Sed - An Introduction and Tutorial

Caveat : for some uncommon character, you may need to force the bracket enclosing to be handled correctly :

# echo "asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr>" | sed 's/<cr>/�/g;s/��*/�/g;s/�/<cr>/g'
asdasdada<cr>aasdsad<cr>asdasdfsdfs<cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr>

---> Here the � has not been handled correctly

Whereas in the 2 following case, it is handled correctly :

# echo "asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr>" | sed 's/<cr>/�/g;s/�[�]*/�/g;s/�/<cr>/g'
asdasdada<cr>aasdsad<cr>asdasdfsdfs<cr>asdsada<cr>adasd<cr>daasdaasd<cr>
# echo "asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr>" | sed 's/<cr>/�/g;s/�[.�.]*/�/g;s/�/<cr>/g'
asdasdada<cr>aasdsad<cr>asdasdfsdfs<cr>asdsada<cr>adasd<cr>daasdaasd<cr>

---------- Post updated at 02:00 PM ---------- Previous update was at 01:53 PM ----------

+ or * refer to the preceding character or [character list] they do NOT refer to the preceeding \(strings\)

---------- Post updated at 02:09 PM ---------- Previous update was at 02:00 PM ----------

@Panyam
Your code is dangerous and may lead to unexpected loss, consider :

# echo "I<cr><cr>like<cr><cr>roasted <cr><cr>apple<cr><cr><cr>crumble<cr>."  | sed 's/<cr>[<cr>]*/<cr>/g'
I<cr>like<cr>oasted <cr>apple<cr>umble<cr>.

Where did go the 'r' of 'roasted' and 'cr' of 'crumble' ?????

:smiley:

I believe below one would help to resolve

sed 's/\(<cr>\)\1*/<CR>/g' inputfile
2 Likes

Hi Michale,

Thanks. You pointed it correctly.

Cheers
Ravi

More straight forward:

# echo 'asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr>'|sed 's/\(<cr>\)\{1,\}/<cr>/g'

result:

asdasdada<cr>aasdsad<cr>asdasdfsdfs<cr>asdsada<cr>adasd<cr>daasdaasd<cr>

Works fine with the POSIX /usr/xpg4/bin/sed,

but doesn't work with the standard Sun sed implementation :

# echo 'asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr>' | sed 's/\(<cr>\)\{1,\}/<cr>/g'
asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr>
# uname -a
SunOS xxxxxxxx 5.10 Generic_141414-01 sun4u sparc SUNW,Sun-Fire-V490

Can you try it with the -e on SunOS?

# uname -a | nawk '{sub($2,"xxxxxxxx",$0)}1'
SunOS xxxxxxxx 5.10 Generic_141414-01 sun4u sparc SUNW,Sun-Fire-V490
# echo 'asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr>' | sed -e 's/\(<cr>\)\{1,\}/<cr>/g'
asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr>
#

... still fail

... this makes me wonder why Sun (Oracle) doesn't just leave out all those old implementation and go with more recent POSIX binaries

1 Like

Modern utilities and a modern shell might break backwards compatibility with someone's 40-year-old pre-POSIX script :wall: So they either won't do it, or would just nail the proper, modern version onto the side in some weird location you'd never guess.

@Corona

Yup, i understand the compatibility logic but ... how about an 'end of support' logic ?

I wish the modern would be the default, and the old would be in some esoteric elsewhere location ...
:smiley: