On the command line using bash, how do you split a string by column?

Input:

MD5(secret.txt)= fe66cbf9d929934b09cc7e8be890522e
MD5(secret2.txt)= asd123qwlkjgre5ug8je7hlt488dkr0p

I want the results to look like these, respectively:

MD5(secret.txt)= fe66cbf9 d929934b 09cc7e8b e890522e
MD5(secret2.txt)= asd123qw lkjgre5u g8je7hlt 488dkr0p

Basically, keeping all the stuff on the left side if equal sign the same, while splitting the stuff after the equal sign into 4 columns of 8 char each. I'm fine with sed or awk.

Any help would be appreciated. Thanks.
.

Need gawk:

awk '$NF=gensub(/(........)/,"\\1 ","g",$NF)' infile
MD5(secret.txt)= fe66cbf9 d929934b 09cc7e8b e890522e
MD5(secret2.txt)= asd123qw lkjgre5u g8je7hlt 488dkr0p

1 Like

Thanks. Just like I wanted. If you don't mind, can you explain to me what the $NF stands for, and also the the gensub?

$NF stands for the last column.

gensub () is gawk string function.

String Functions - The GNU Awk User's Guide

Alternatively, using Perl:

$ 
$ 
$ cat f7
MD5(secret.txt)= fe66cbf9d929934b09cc7e8be890522e
MD5(secret2.txt)= asd123qwlkjgre5ug8je7hlt488dkr0p
$ 
$ perl -lne '/^(.*= )(.*)$/; @x=unpack("A8"x4,$2); print "$1@x"' f7
MD5(secret.txt)= fe66cbf9 d929934b 09cc7e8b e890522e
MD5(secret2.txt)= asd123qw lkjgre5u g8je7hlt 488dkr0p
$ 
$ 

tyler_durden

Or POSIX sed:

sed 'h; s/.* //; s/.\{8\}/& /g; x; s/[^ ]*$//; G; s/\n//'

can you explain me your code

thank's

h                          # The "h" command copies the pattern buffer into the hold buffer. The pattern buffer is unchanged. 
s/.* //                   # get the content after last space.
s/.\{8\}/& /g          # add space for every 8 chars.
x                          # The "x" command exchanges the hold buffer and the pattern buffer. 
s/[^ ]*$//             # get the content before last space.
G                         # Instead of exchanging the hold space with the pattern space, you can copy the hold space to the pattern space with the "g" command. This deletes the pattern space. If you want to append to the pattern space, use the "G" command. This adds a new line to the pattern space, and copies the hold space after the new line.
s/\n//                   # remove the new line which added by G.
1 Like

rdcwayx thank's

my $str='MD5(secret.txt)= fe66cbf9d929934b09cc7e8be890522e';
$str=~s/(?=(\S{8})+$)/ /g;
print $str;

sed 'h; s/.* //; s/.\{8\}/& /g; x; s/[^ ]*$//; G; s/\n//' file1

please explain this code many thing I have not understand or aware of:-)

RahulJoshi,

rdcwayx explained it above in post #8.