append txt files and format data

September 13, 2010 at 18:50:06
Specs: Windows 7
Hello, i have thousand txt files called "Posizioni_Number1_Number2.txt", eg. Posizioni_2160_004.txt.
The numbers are always multipliers of 4, so surely there is no file called Posizioni_2161_005.txt. And the second number goes from 000 to 356.
In each of them data are formatted like this:
nodenumber x-coordinate y-coordinate x-velocity y-velocity 1 3.310E-001 3.945E-001 4.694E+000 7.7593E-001
all in a unique line and separated by several spaces.
I need a batch file, or something else, that in input accepts the endpoints for the first number (eg 2160 2220) and, for each file (maybe with a double for loop incrementing by 4), takes only last 4 numbers inside (ignoring all text and the first number) and append them in a new txt file named like the first number in the txts names.
So if i have files like "Posizioni_2160_004.txt", "Posizioni_2160_008.txt", "Posizioni_2160_12.txt" should be created one file named 2160.txt with corresponding data placed sequentially. But for other files like "Posizioni_2172_004.txt", "Posizioni_2172_008.txt" "Posizioni_2172_12.txt" a new file must be created, named 2172.txt. and this repeated for each file between the endpoints given in input.
But it's not over!:)
I need that if a file with a specific second number in the name does not exist (eg Posizioni_2160_008.txt), instead of the 4 numbers, in the corresponding file (2160.txt), in the right place ( after the 2160_004 data) , must be inserted four NaN.
Can anyone help me? I know it's a mess-.-'..but this script could save me days of boring and repetitive job! I hope i was clear enough and sorry for my bad english.
Thanx!
Alessandro


See More: append txt files and format data

Report •

#1
September 13, 2010 at 20:56:44
if you can download Python, you can use this script

import os
import glob
import sys
start=int(sys.argv[1])
end=int(sys.argv[2])
RANGE=range(start,end)
crange=[str(i).zfill(3) for i in range(0,357,4) ]
root="C:\\"
DIR=os.path.join(root,"test")
for files in glob.glob("Posizioni*"):
   s=files.split("_")
   if s[1] in RANGE:
       if s[-1].split(".")[0] in crange:
          toget = open(files).readline().split()[-4:]
          open(s[1]+".txt","a").write( ' '.join(toget)+"\n"  )

usage:
c:\test> python myscript.py 2160 2200

GNU win32 packages | Gawk


Report •

#2
September 14, 2010 at 03:44:36
sorry but nothing happened.. even if i copied all Posizioni_X_X.txt files in C:\test
why?
thanks

Report •

#3
September 14, 2010 at 07:45:58
i suspect you are running it from another directory. if that's the case, do a chdir() first
.......
DIR=os.path.join(root,"test")
os.chdir(DIR)
for files in glob.glob("Posizioni*"):
   ......

GNU win32 packages | Gawk


Report •

Related Solutions

#4
September 15, 2010 at 15:14:00
thank you! it works someway. now i'll try to apply some secondary changes. bye!

Report •

Ask Question