sed regexp teaser

G'day,

Here's a teaser for a sed guru, which I surely am not one, as even my
basic sed skills are rusted from years of not practising ... lol

Ok ... we have a string of digits such as:

632413741610252847552619172459483022433027602515212950543016701812771409213148672112

we want it split into rows containing three two digits tuples that
are space separated, as follows for the example above:

63 24 13
74 16 10
25 28 47
55 26 19
17 24 59
48 30 22
43 30 27
60 25 15
21 29 50
54 30 16
70 18 12
77 14 09
21 31 48
67 21 12

Yes, I could do this easily in perl or awk (if I had even bothered, as even
these I have rusted out on) but I wanted to refresh my sed and found myself
stuck at how to compose such a beast ...

So, I'm obviously after the most elegant and least convoluted solution that you may come up with ... no hurry either! :wink:

Thanks in advance

Robert Nader

sed -e 's#\(..\)\(..\)\(..\)#\1 \2 \3\n#g' file

sledge-hammer:

echo '632413741610252847552619172459483022433027602515212950543016701812771409213148672112' | sed 's/\(..\)\(..\)\(..\)/\1 \2 \3\n/g'

Standard sed

sed '
s/\(..\)\(..\)\(..\)/\1 \2 \3\
/g
' file

If you want to get rid of the last newline, the effort is much higher.
Here is an attempt

sed '
/\(..\)\(..\)\(..\)\(.\)/{
s//\1 \2 \3\
\4/
P;D
}
s/\(..\)\(..\)\(..\)/\1 \2 \3/
' file

I was surprised that no loop is needed. The explanation is that D jumps to the next cycle if there is another line, and the insertion of a newline has just created it!

G'day,

Thanks to all of you for your very prompt replies.

A true pack of wizards ... the lot of you! :wink:

Particular thanks go to Yoda as just last night my 6 y/o daughter and I were humming the Empire Strike tune ( I think it's that one) and when I asked her
who was her favourite character ... she said Yoda! :wink:

Cheers

Robert Nader

Hi.

For the string in file z8, also this in place of sed :

fold -2 z8 | paste -d ' ' - - -

producing:

63 24 13
74 16 10
25 28 47
55 26 19
17 24 59
48 30 22
43 30 27
60 25 15
21 29 50
54 30 16
70 18 12
77 14 09
21 31 48
67 21 12

On a system like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.9 (jessie) 
bash GNU bash 4.3.30
fold (GNU coreutils) 8.23
paste (GNU coreutils) 8.23

Best wishes ... cheers, drl

G'day!

fold? paste?

$ type -a fold paste
fold is /usr/bin/fold
paste is /usr/bin/paste
$ man fold paste

both are part of coreutils, ... Wow!

I don't recall knowing these handy tools!
I'm sure I must have glanced over them years ago
when I used to be more into this stuff, but if you
don't ever use them you may not even know these exist!

Yes, the general UNIX toolkit is full of useful, interesting and on-point
utilities!!!

$ echo '632413741610252847552619172459483022433027602515212950543016701812771409213148672112' | fold -2 | paste -d ' ' - - -

63 24 13
74 16 10
25 28 47
55 26 19
17 24 59
48 30 22
43 30 27
60 25 15
21 29 50
54 30 16
70 18 12
77 14 09
21 31 48
67 21 12

Thanks again UNIX masters!

1 Like

Just for fun, another one:

sed 's/../& /g' file | xargs -n 3

Hi,

$ echo '632413741610252847552619172459483022433027602515212950543016701812771409213148672112' | sed 's/../& /g' | xargs -n 3
63 24 13
74 16 10
25 28 47
55 26 19
17 24 59
48 30 22
43 30 27
60 25 15
21 29 50
54 30 16
70 18 12
77 14 09
21 31 48
67 21 12

Wicked use of xargs --max-args with no actual command, very clever!!! :wink:

I'm certainly getting a great refresher here!

Thanks!

Please, notice these two variations:

echo '6324137416102528475526191724594830224330276025152129505430167018127714092131486721126' | perl -ple 's/(..)(..)(..)/$1 $2 $3\n/g'
63 24 13
74 16 10
25 28 47
55 26 19
17 24 59
48 30 22
43 30 27
60 25 15
21 29 50
54 30 16
70 18 12
77 14 09
21 31 48
67 21 12
6
$ echo '6324137416102528475526191724594830224330276025152129505430167018127714092131486721126' | perl -nle 'while(/(..)(..)(..)/g){print "$1 $2 $3"}'
63 24 13
74 16 10
25 28 47
55 26 19
17 24 59
48 30 22
43 30 27
60 25 15
21 29 50
54 30 16
70 18 12
77 14 09
21 31 48
67 21 12