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 ----------
skpvalvekar:
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.
+ 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' ?????
ctsgnb:
@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' ?????
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>
ctsgnb
June 9, 2011, 9:56am
10
freegnu:
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
ctsgnb:
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?
ctsgnb
June 9, 2011, 10:20am
12
# 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.
ctsgnb
June 9, 2011, 11:38am
14
@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 ...