-2

I would like to edit something like this;

ABC
abc
123

Into something like this;

Aa1Bb2Cc3
2
  • 1
    the number of characters in each line are the same
    – eliezer
    Jun 9 at 4:03
  • please edit and add that to your question plus your other comment under accepted answer Jun 10 at 6:37

3 Answers 3

1

Using 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
0

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.

0
0

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 combed 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .