Copying a specific column from one text file to another

December 20, 2012 at 05:24:37
Specs: Windows 7

Guys,

I have a bunch of .txt files from which i want to copy a specific column (from all of them) into one .txt file. The output.txt file needs to be created

For eg.

file1.txt
a b c
j k l

file2.txt
d e f
m n o

file3.txt
g h i
p q r

output.txt
b e h
k n q

For the same I am looking for a batch file in windows that can help me achieve this. Any kind of help would be really appreciated :). I am a mere rookie in batch scripting and hence pardon me if this sounds a very basic problem.

Thanks a ton in advance,
Neel


See More: Copying a specific column from one text file to another

Report •


#1
December 20, 2012 at 15:21:23

Maybe...

@echo off & setlocal enabledelayedexpansion
for %%v in ($ # line line2) do set %%v=
for /f "tokens=*" %%f in ('dir /b /a-d *.txt') do (
	for /f "tokens=2 delims= " %%i in (%%f) do (
		set /a #+=1
		set /a $="!#!" %% "2"
		if not "!$!"=="0" (
			set line=!line! %%i
		) else (
			set line2=!line2! %%i
		)
	)
)
set line=!line:~1!
set line2=!line2:~1!
(
echo !line!
echo !line2!
)>>output.txt

Tony


Report •

#2
December 20, 2012 at 17:31:42

I guess my question is, are these files sequence-numbered like in your example, and if so, the format of the naming. f/e, you could have: file1.txt, file01.txt, file001.txt etc, you might have thisfil1.txt, thatfile2.txt, n3.txt etc. (Assuming that the order of the files is significant) (and if this is the case you might want to add: "/o:n" to the "dir" parameters in Tony's script).
And are they all two lines long?
Hopefully the script Tony provided will make my questions irrelevant and moot.
ps: Your example was excellent help, I would have been totally confused without it.

Report •

#3
December 21, 2012 at 01:51:07

@Tony, thanks a lot for your input. Your script works fine for a txt file with 2 lines. But my files are pretty huge have 48002 lines to be exact :). If i use a file with more than 2 lines, the output.txt is all jumbled up. Any suggestions on how to fix this?

@nbrane: the name of my files are arranged in a sorted order. Again to be exact, I have several bunch of 96 files named something like this: "NREL5MW_Monopile_RF_UpWindBlade_1.out" .......NREL5MW_Monopile_RF_UpWindBlade_96.out.

But as I pointed out, they are not 2 lines long, perhaps its my fault, I should have made it clearer before.


Report •

Related Solutions

#4
December 21, 2012 at 13:35:15

Hence, my questions. Them being addressed, maybe this will work, but i don't know how long it will take with the given spec.s (96 files, 48k lines each):

::======= begin batch
@echo off>output & setlocal enabledelayedexpansion
set pre=NREL5MW_Monopile_RF_UpWindBlade
set sk=
for /L %%i in (0 1 48002) do (
set line=
for /L %%a in (1 1 96) do (
set fil=%pre%_%%a.out
if "%%i" gtr "0" set sk=skip=%%i
call :zz
)
>> output echo !line!
)
goto :eof

:zz
for /f "%sk% tokens=2" %%b in (%fil%) do set line=!line!%%b & goto :eof
::===== end batch


Report •

#5
December 26, 2012 at 12:36:44

I think this problem is best solved with a 2-dimensional array which might be too difficult to perform in batch? How about a perl program? So, for each line in each file, print out the second character. This perl program reads in each file into an array-of-arrays. See this link for a discussion:

http://docstore.mik.ua/orelly/perl3...

Execute the perl program with the files as command line arguments:

prog.pl file1.txt file2.txt file3.txt

While this was developed on Linux, it should be portable to a WIndows platform with perl:

#!/usr/local/bin/perl  -w

my $myfile="";
my $myline=0;
my $file_str;
my @file_array;
my $i=1;
while ( <> ) {
    chomp;
    my $mydata = $_;
    if ($myfile ne $ARGV)
        {
        $myline=0;
        $myfile = $ARGV;
        $file_str = "file" . $i++;
        push(@file_array, $file_str);
        }

        $myline++; # count the lines in any given file
        $HoH{$file_str}{$myline} = $mydata;
    }

@file_array = sort(@file_array);
my @myvalue;
my $mycol=1; # arrays start counting at 0 - not 1
foreach $cnt (1 .. $myline) {
   foreach $listfile (@file_array) {
       @myvalue = split(/ /, $HoH{$listfile}{$cnt});
        print "$myvalue[$mycol] ";
       }
      print "\n"; # new line after each output line generated
   }

# let me know if you have any questions.


Report •


Ask Question