-
Notifications
You must be signed in to change notification settings - Fork 24
/
extract_column_from_tab_separated_text_file.praat
93 lines (73 loc) · 2.55 KB
/
extract_column_from_tab_separated_text_file.praat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# This script extracts a tab-separated column from a text file.
#
# This script is distributed under the GNU General Public License.
# Copyright 22.5.2003 Mietta Lennes
form Extract column from a tabular text file
sentence Column_name Duration
comment Give the path of the text file:
text Input_file /home/lennes/selection.txt
comment Give the path of the resulting file:
text Output_file /home/lennes/durations.txt
endform
Read Strings from raw text file... 'input_file$'
Rename... temp
if fileReadable(output_file$)
pause File 'output_file$' exists! Do you want to overwrite it?
filedelete 'output_file$'
endif
call GetColumnNumber col 'column_name$'
if col > 0
numberOfLines = Get number of strings
for line from 2 to numberOfLines
call GetColumnFromTextLine col line string$
fileappend 'output_file$' 'string$''newline$'
endfor
else
exit Column 'column_name$' was not found!
endif
printline The column 'column_name$' was extracted to file 'output_file$'.
#-------
# This procedure finds a specified tab-separated string from the first line in
# a Strings object, and saves the column number to variable 'tovariable$'.
procedure GetColumnNumber tovariable$ text$
col = 0
column_title_not_found = 0
repeat
col = col + 1
call GetColumnFromTextLine col 1 string$
until string$ = text$ or lineend = 1
if string$ = text$
'tovariable$' = col
else
'tovariable$' = 0
column_title_not_found = 1
endif
if column_title_not_found = 1
printline For your attention, "'text$'" was not available.
endif
endproc
#--------------
# This procedure reads a tab-separated string from a Strings object
# and returns the string within a given column in a variable.
procedure GetColumnFromTextLine col line output$
data$ = Get string... line
column = 1
lineend = 0
while column < col and lineend = 0
column = column + 1
if index (data$, " ") > 0
data$ = right$ (data$, (length (data$) - index (data$, " ")))
else
data$ = ""
lineend = 1
endif
endwhile
if index (data$, " ") > 0
data$ = left$ (data$, (index (data$, " ") - 1))
endif
if right$ (output$, 1) = "$"
'output$' = data$
else
'output$' = extractNumber (data$, "")
endif
endproc