Merge columns of data from multiple text files by row from each seperate file using Powershell -
Merge columns of data from multiple text files by row from each seperate file using Powershell -
i have output numerical modelling code. needed extract specific value series of files. used next code (i derived illustration extract ip addresses logfiles):
$input_path = ‘c:\_test\input_pc\out5.txt’ $output_file = ‘c:\_test\output_pc_all\out5.txt’ $regex = ‘\bhead(.+)\s+[\-]*\d{1,3}\.\d{6,6}\s?\b’ select-string -path $input_path -pattern $regex -allmatches | % { $_.matches } | % { $_.value } > $output_file so have got number of text files contain measurements (the number of files may variable, there 50) 1 column of numeric info (with number of rows equals 7302 may vary depending on length of time series modelled) , may positive or negative per illustration info below.
note semicolon preceding text indicates follows comment using explain order of dataset , not appear in info processed...
out1.txt
-1.000000 ; 1st line of out1.txt 2.000000 ; 2nd line of out1.txt -3.000000 ; 3rd line of out1.txt ... 5.000000 ; nth line of out1.txt
out2.txt
-1.200000 ; 1st line of out2.txt -2.200000 ; 2nd line of out2.txt 3.200000 ; 3rd line of out2.txt ... -5.20000 ; nth line of out2.txt
outn.txt
1.300000 ; 1st line of outn.txt -2.300000 ; 2nd line of outn.txt -3.300000 ; 3rd line of outn.txt ... 10.300000 ; nth line of outn.txt
i need merge them single text file (for illustration lets phone call "combined_output.txt") using powershell info ordered first row of values differing output files appear first, repeat row 2 , on below:
combined_output.txt
-1.000000 ; 1st line of out1.txt -1.200000 ; 1st line of out2.txt 1.300000 ; 1st line of outn.txt 2.000000 ; 2nd line of out1.txt -2.200000 ; 2nd line of out2.txt -2.300000 ; 2nd line of outn.txt -3.000000 ; 3rd line of out1.txt 3.200000 ; 3rd line of out2.txt -3.300000 ; 3rd line of outn.txt ... 5.000000 ; nth line of out1.txt -5.200000 ; nth line of out2.txt 10.300000 ; nth line of outn.txt
just i'm new sort of thing hope explanation above makes sense , help can provide much appreciated.
edit having run models, when using code big info files created, there seems issue of sorting of imported data. seems occur when there repeated values illustration sec row of info each outfile has been combined in next order script. looks there sorting based on value of info , not based on out file name:
value ; out file text number -1.215809 ; 1 -0.480543 ; 18 -0.480541 ; 19 -0.48054 ; 2 -0.480539 ; 20 -0.480538 ; 21 -0.480537 ; 22 -0.480536 ; 23 -0.480535 ; 24 -0.480534 ; 25 -0.480534 ; 26 -0.480688 ; 10 -0.480533 ; 27 -0.480532 ; 3 -0.480776 ; 4 -0.48051 ; 5 -0.48051 ; 6 -0.48051 ; 7 -0.48051 ; 8 -0.48051 ; 9 -0.48051 ; 11 -0.48051 ; 12 -0.48051 ; 13
i sense might have on complicated reply lets see how do. consider next dummy info similar samples
out1.txt out2.txt out3.txt -0.40000 0.800000 4.100000 3.500000 0.300000 -0.90000 -2.60000 0.800000 2.200000 0.500000 1.800000 -1.40000 3.600000 1.800000 1.400000 40000000 -0.70000 1.500000 the file contents arranged side side reply brevity , help understand output. code follows:
$allthefiles = @() get-childitem c:\temp\out*.txt | foreach-object{ $allthefiles += ,(get-content $_.fullname) } ($lineindex=0; $lineindex -lt $allthefiles[0].count; $lineindex++){ for($fileindex=0; $fileindex -lt $allthefiles.count; $fileindex++){ $allthefiles[$fileindex][$lineindex] } } | out-file -filepath c:\temp\file.txt -encoding ascii gather out*.txt files code creates array of arrays file contents themselves. using nested for loops cycle though each single file outputting 1 line each file @ time. while having hard time beingness clear on happening if compare sample info output should see first line or every file outputted followed next line...etc.
this code produce next output
-0.40000 0.800000 4.100000 3.500000 0.300000 -0.90000 -2.60000 0.800000 2.200000 0.500000 1.800000 -1.40000 3.600000 1.800000 1.400000 40000000 -0.70000 1.500000 caveats
the code assumes files of same size. number of lines determined first file. if other files contain more info lost in model.
powershell
Comments
Post a Comment