0

I will give an example where N = 3

The input looks like this:

A
B
C

D
E
F

G
H
I

The output must look like this:

A     D     G

B     E     H

C     F     I

(the input might have 12 lines instead of 9 though. Or 30, or 300..)

But most of the time I need to join, in the selected text, the top half with the bottom half. In other words, I select N lines and I need to join the first N/2 lines with the last N/2 lines. I will give an example:

The input looks like this:

A
B
C
D

1
2
3
4

The output must look like this:

A     1
B     2
C     3
D     4

(In this case the value of N was 8)

Is it possible to do something like that using regex or macros?

Note, there is a similar question here, but that's completely distinct to this one though

2 Answers 2

2

It is possible to do in a text editor. Text editor which has "vertical blocks" selection.

  • select vert-block for one column
  • Cut to clipboard
  • move caret to desired pos in first line
  • Paste from clipboard

(For ex, I use CudaText. It has vertical blocks; allows cursor after line-end, so no problem to paste at desired pos).

1
  • 1
    Indeed, thanks. Although it would be nice to have an interface to simply ask you for the value of N and then to do the rest of the work, for the currently selected text. But, most likely, I would have to write a plugin in order to get that.
    – DreamCode
    Commented Aug 30, 2018 at 13:22
1

On unix, you can use the command line tools head, tail and paste.

       paste - merge lines of files

       Write lines consisting of the sequentially corresponding lines 
       from each FILE, separated by TABs, to standard output.

The solution sketched below might require some finetuning from your side.

Assuming your input

A
B
C
D

1
2
3
4

is stored in file1.txt,

do this:

head -5 file1.txt > f1.txt ; tail -n +6 file1.txt > f2.txt

to write them out in two files.

Then do

paste f1.txt f2.txt

Result:

A   1
B   2
C   3
D   4

You can do all on one line with

n=9; head -$((n/2)) file1.txt  > f1.txt ; tail -n +$((n/2 + 2)) file1.txt  > f2.txt; paste f1.txt f2.txt
2
  • Thanks, I'm using GNUWin32 for using unix tools on Windows. It would be more interesting if the script would just ask you the value of N and then do the rest of the job though.
    – DreamCode
    Commented Aug 30, 2018 at 15:37
  • 1
    the one-liner has a single parameter n=9 which you can change according to your wishes, e.g. to n=256 to write fetch+split the first 256 lines from your infile.
    – knb
    Commented Aug 30, 2018 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.