Search code examples
windowsunixpasteregexp-replace

reg expression / search and replace to simulate paste command of unix


if i have a file

a
b
c
d
e

is there a search and replace / reg expr / cli that can produce

a a
b b
c c
d d
e e

i know the unix command paste does it but I am in a locked down environment on windows.

thanks


Solution

  • Given the file sample.txt, with each line ending in a newline:

    a
    b
    c
    d
    e
    

    The following line run at a command prompt will produce "result.txt" with the output you want:

    for /f %i in (sample.txt) do echo %i %i >> result.txt
    

    The for loop reads each line in the input file, assigns it to the variable %i, and then echos that variable twice, appending the output via redirection to the output file. To use in a batch file, double the percent sign before each use of the variable i, as in %%i.

    Typing for /? or help for at a command prompt will provide you with more information about use of that command.