Using Raku (formerly known as Perl_6)
[Adds extra blank line at end]:
raku -ne 'my ($L1,$L2,$L3,$L4) = (get) xx 4; .put for $L3, $L1, $L2, $_, ($L4 // "");'
#OR
raku -e 'my @a.=push($_) if .chars for lines; put($_ ~ "\n") for @a.rotor(4)>>.[3,1,2,0]>>.join("\n");'
[Doesn't add extra blank line at end]:
raku -ne 'my ($L1,$L2,$L3,$L4) = (get) xx 4; .put for $L3, $L1, $L2, $_; $L4.defined ?? $L4.put !! next;'
#OR
raku -e 'my @a.=push($_) if .chars for lines; .put for @a.rotor(4)>>.[3,1,2,0]>>.join("\n").join("\n\n");'
First and third examples are similar to Perl5 code posted by @ForeverCuber, and bears a vague resemblance to awk
code posted by @RudiC. First/third examples use -ne
commandline flags. Second/fourth examples push line
s onto an @
-sigiled array.
For the first example the -ne
commandline flags means the first line loads into $_
, Raku's topic variable. Raku is instructed to get
4 successive lines, which load into each of ($L1,$L2,$L3,$L4)
. The Record Separator is represented by $L4
which, when undefined (at end), is handled using Raku's //
defined-OR operator. Thus ""
blank string is output
if left-side of //
evaluates to undefined. Printing is simplified without any join
s and by the use of put
only.
Second example push
es line
s onto an array, @a
. Blank lines are skipped via the conditional if .chars
. Array elements are rotor
ed together (grouped into 4-element sublists/records), reordered with [3,1,2,0]
indexing, and join
ed with one call to join("\n")
. Printing of records is simplified by use of put($_ ~ "\n")
, which demonstrates that string concatenation is accomplished via ~
tilde in Raku.
Third example is similar to first example in that it uses -ne
commandline flags, and again, no join
s are used. However undefined last line is handled better by Raku's "Test ??
True !!
False" ternary operator (i.e. $L4.defined ?? $L4.put !! next;
). Thus no extra blank line is added at the end. Note: print ""
could be used here, in place of next
.
Fourth example is similar to second example, except output
is built up by two successive calls to join
, which recreates the original record spacing (and no extra blank line is added at the end). The first >>.join("\n")
call maps into the rotor
ed record to join the 4 lines into a single array element. The second .join("\n\n")
takes the resultant 5 array elements (records) and joins them with a blank space in-between each, recreating the input but with the desired line-ordering.
Sample Input (lines have been numbered):
1 DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
2 changetype: modify<br />
3 delete: DelegatedAdmin<br />
4 dn: cn=311999,ou=Entities,ou=Active,ou=Vault<br />
5 DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
6 changetype: modify<br />
7 delete: DelegatedAdmin<br />
8 dn: cn=948089,ou=Entities,ou=Active,ou=Vault<br />
9 DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
10 changetype: modify<br />
11 delete: DelegatedAdmin<br />
12 dn: cn=947458,ou=Entities,ou=Active,ou=Vault<br />
13 DelegatedAdmin: cn=9776c3f8-f2ee-4b6c-a9a9-32d87b45f7c5,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
14 changetype: modify<br />
15 delete: DelegatedAdmin<br />
16 dn: cn=10717,ou=Entities,ou=Active,ou=Vault<br />
17 DelegatedAdmin: cn=e0a838f0-3d53-42e8-8dad-bd1d774381de,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
18 changetype: modify<br />
19 delete: DelegatedAdmin<br />
20 dn: cn=1500,ou=Entities,ou=Active,ou=Vault<br />
Sample Output:
4 dn: cn=311999,ou=Entities,ou=Active,ou=Vault<br />
2 changetype: modify<br />
3 delete: DelegatedAdmin<br />
1 DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
8 dn: cn=948089,ou=Entities,ou=Active,ou=Vault<br />
6 changetype: modify<br />
7 delete: DelegatedAdmin<br />
5 DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
12 dn: cn=947458,ou=Entities,ou=Active,ou=Vault<br />
10 changetype: modify<br />
11 delete: DelegatedAdmin<br />
9 DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
16 dn: cn=10717,ou=Entities,ou=Active,ou=Vault<br />
14 changetype: modify<br />
15 delete: DelegatedAdmin<br />
13 DelegatedAdmin: cn=9776c3f8-f2ee-4b6c-a9a9-32d87b45f7c5,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
20 dn: cn=1500,ou=Entities,ou=Active,ou=Vault<br />
18 changetype: modify<br />
19 delete: DelegatedAdmin<br />
17 DelegatedAdmin: cn=e0a838f0-3d53-42e8-8dad-bd1d774381de,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
https://raku.org