Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert ###[:alpha;] to ###\n[:alpha;] ?

19 views
Skip to first unread message

Bit Twister

unread,
Oct 22, 2016, 8:15:40 AM10/22/16
to
I want to add a new line character between a variable length string of
## and text in a log file.

Three short example strings:
####warning
####Error
####Fail

My sed attempts seem to upchuck on the # character. I would like to use
sed or awk rather than a higher level app like perl or python if possible.

Thank you in advance for any help.

Rakesh Sharma

unread,
Oct 22, 2016, 8:25:43 AM10/22/16
to
On Saturday, 22 October 2016 17:45:40 UTC+5:30, Bit Twister wrote:

>
> I want to add a new line character between a variable length string of
> ## and text in a log file.
>
> Three short example strings:
> ####warning
> ####Error
> ####Fail
>
> My sed attempts seem to upchuck on the # character. I would like to use
> sed or awk rather than a higher level app like perl or python if possible.
>

sed -e 's/##*/&\n/g' yourfile

Janis Papanagnou

unread,
Oct 22, 2016, 8:26:39 AM10/22/16
to
Subject: Re: convert ###[:alpha;] to ###\n[:alpha;] ?

On 22.10.2016 14:15, Bit Twister wrote:> I want to add a new line character
between a variable length string of
> ## and text in a log file.
>
> Three short example strings:
> ####warning
> ####Error
> ####Fail
>
> My sed attempts seem to upchuck on the # character.

Impossible to tell what's wrong without seeing your sed command.

> I would like to use
> sed or awk rather than a higher level app like perl or python if possible.

Try whether your sed version works for you with

sed 's/\(###\)\([[:alpha:]]\)/\1\n\2/'

(Assuming you don't want a literal "\n" but a newline character, otherwise
prepend a '\' to '\n'.)

Janis

Janis Papanagnou

unread,
Oct 22, 2016, 8:28:25 AM10/22/16
to
This will also replace matched patterns with non-alpha suffixes.

Janis

Bit Twister

unread,
Oct 22, 2016, 9:31:50 AM10/22/16
to
On Sat, 22 Oct 2016 14:26:37 +0200, Janis Papanagnou wrote:
> Subject: Re: convert ###[:alpha;] to ###\n[:alpha;] ?
>
> On 22.10.2016 14:15, Bit Twister wrote:> I want to add a new line character
> between a variable length string of
>> ## and text in a log file.
>>
>> Three short example strings:
>> ####warning
>> ####Error
>> ####Fail
>>
>> My sed attempts seem to upchuck on the # character.
>
> Impossible to tell what's wrong without seeing your sed command.

Yes, thought about posting my attempts, but was embarrassed on how bad
the attempts were and did not want to waste anyone's time telling why
they didn't work.


>> I would like to use
>> sed or awk rather than a higher level app like perl or python if possible.
>
> Try whether your sed version works for you with
>
> sed 's/\(###\)\([[:alpha:]]\)/\1\n\2/'

Works perfectly as desired.


Thank you for all your time spent helping people in this group.

Rakesh Sharma

unread,
Oct 22, 2016, 4:10:03 PM10/22/16
to
Yep that's correct. I didn't look at the subject line of the OP.
Merely reading the body indicated that the OP wants to separate
the string of hashes and text with a newline.

Rakesh Sharma

unread,
Oct 22, 2016, 4:11:46 PM10/22/16
to
For a POSIX sed the above should be recast as:
sed 's/\(###\)\([a-zA-Z]\)/\1\
\2/'

Geoff Clare

unread,
Oct 24, 2016, 9:11:08 AM10/24/16
to
Rakesh Sharma wrote:

>> > sed 's/\(###\)\([[:alpha:]]\)/\1\n\2/'

> For a POSIX sed the above should be recast as:
> sed 's/\(###\)\([a-zA-Z]\)/\1\
> \2/'

The [[:alpha:]] syntax is POSIX, but you are right about it needing
a real newline (escaped with \) instead of \n.

--
Geoff Clare <net...@gclare.org.uk>
0 new messages