Copy a specific column from one text file to another

I have a bunch of .txt files from which I want to copy a specific column (of all of them) into a single .txt file. An output.txt file must be created.

For instance,

file1.txt abc jkl file2.txt def mno file3.txt ghi pqr output.txt beh knq 

For the same, I'm looking for a batch file on Windows that can help me with this. We will be very grateful for any help :). I'm just new to the batch script and therefore apologize if this sounds like a very simple problem.

+4
source share
3 answers

This may not be the answer you are looking for, but I had a similar, though not exact, problem when working with strings in batch files. I was so upset that I finished learning and using python.

In Python, it will be as simple as:

 for i in range(1,4): f='c:\\file'+str(i)+'.txt' # Creates a variable for file1.txt, file2.txt formatted as c:\file1.txt. Path can be changed as needed. f = open(f) # Opens the file string=f.read() # Adds the contents of the file to a string print(string.split('\t')[1]+'\t'+string.split('\t')[4]) # splits the string by tabs, and returns the 2nd and 5th item. 

This displays it, and here you can write it to a file.

+1
source

This batch decision will take the 2nd char of all lines from each file in the folder and output them to output.txt , wow! :)

 for %%a in (file*.txt) do ( for /f "tokens=2 delims= " %%b in (%%a) do echo %%b >>output.txt ) 

Getting them in a line will be more difficult, is it strictly necessary or maybe in the list, one char per line?

+1
source

Edit: another way to do this (perhaps more straight forward), this has a different limit - the size of the available variable / env. space (about ~ 32 kB for XP, not limited to Vista up - at least for MS docs). It creates the appearance of array variables to hold your transposed lines, and then displays them:

 @echo off setlocal enabledelayedexpansion set "tab= " for %%F in (file*.txt) do ( set /a count=0 for /f "tokens=2 delims=%tab%" %%L in (%%F) do ( set /a count+=1 for /L %%T in (!count!,1,!count!) do ( set L[!count!]=!L[%%T]!%%L%tab% ) ) ) for /L %%L in (1,1,%count%) do echo !L[%%L]! 

First, let me say that batch not suitable for this task (which would be trivial, say, in C ), but if you need to use it, here is the way:

 @echo off setlocal enabledelayedexpansion set "tab= " for /f %%C in ('dir /b file*.txt') do set /a count+=1 (for %%F in (file*.txt) do ( set line=0 for /f "tokens=2 delims=%tab%" %%V in (%%F) do ( set "outline=%%F" for /l %%N in (1,1,!line!) do (set "outline=%tab%!outline!") set outline=!outline!%tab%%%V set /a line+=1 echo !outline! ) )) >presorted.txt set /a cutoff=%count%-1 set line=0 for /f "tokens=2 delims=%tab%" %%O in ('sort /r presorted.txt') do ( set outp=%%O%tab%!outp! set /a lc=!line!^%%count% if !lc!==%cutoff% ( echo !outp! set "outp=" ) set /a line+=1 ) 

How it works:

  • Fist, check how many files will be processed. This gives the number of columns. (First for and count variable)
  • Read each input file, edit the required column so that it is preceded by the number of tabs, equal to the line number and file name, in the intermediary file. (Second block bounded by () )
  • Sort this file so that consecutive lines correspond to consecutive columns in the final release ( sort /r presorted.txt )
  • Move blocks of rows into columns with modulo count to start a new row (last for ).

Notes:

  • the order of the columns depends on the sort order of the file names.
  • all files must have the same number of lines
  • "tab= " (in the third line) should contain the actual tab
  • it accepts ascii-ish encoding (where tab sorted before any printable character).
  • it does not accept any characters ! in inputs (they require special processing when slow expansion is enabled)
  • is displayed on the screen. If file output is required, put the code in a batch file, and then run mybatchfile.bat >output.txt
0
source

All Articles