I would like to edit something like this;
ABC
abc
123
Into something like this;
Aa1Bb2Cc3
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityUsing any awk:
awk '
{ a[NR] = $0 }
END {
n = length(a[1])
for (i=1; i<=n; i++) {
for (j=1; j<=NR; j++) {
printf "%s", substr(a[j],i,1)
}
}
print ""
}
' file
Aa1Bb2Cc3
Asumming the string are exactly 3 characters long you can do it with awk
for example. Something like do the work:
awk 'BEGIN {FS="" } {a=a$1;b=b$2;c=c$3} END {print a b c}' input_file >output_file
If the length is different and constant over the lines you can use something like:
awk -v N=3 'BEGIN {FS="" } { for(i=1;i<=N;i++) a[i]=a[i]$i} END { for(i=1;i<=N;i++) {printf "%s",a[i]};}' input_file >output_file
Replace 3
with the number of chars per line
This will work well on GNU awk
. Not sure about the rest of variant.
Using Raku (formerly known as Perl_6)
~$ raku -e 'my @a = lines.map: *.comb(); put join "", [Z] @a;' file
#OR
~$ raku -e 'my @a = lines.map: *.comb(); put join ",", [Z] @a;' file
The first code example joins all letters together. The second code example joins on commas. Quickly, lines
are read in and each line is comb
ed into single character elements. These are stored in @a
array. To output
the file, each element of the array is [Z]
"zip" combined/reduced such that all first elements are taken off and combined, all second elements are taken off and combined, etc.
Sample Input:
ABC
abc
123
Sample Output (first code example):
Aa1Bb2Cc3
Or you can remove the join ",",
call and ther return will be joined on single-spaces:
~$ raku -e 'my @a = lines.map: *.comb(); put [Z] @a;' file
A a 1 B b 2 C c 3
https://docs.raku.org/language/operators#Zip_metaoperator
https://raku.org