-
Notifications
You must be signed in to change notification settings - Fork 0
/
mas_coa.ado
78 lines (60 loc) · 2.47 KB
/
mas_coa.ado
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
* Description: This is a sub-program for mas.ado, aiming at copying or appending text files from one place to another.
* Author: Meiting Wang, doctor, Institute for Economic and Social Research, Jinan University
* Email: wangmeiting92@gmail.com
* Created on July 18, 2021
* Updated on July 25, 2021
program define mas_coa
version 16.0
syntax using/, saving(string) [replace append Lines(numlist integer >0 ascending)]
*-----------------前期程序---------------------
* 错误信息处理
if ("`replace'" != "") & ("`append'" != "") {
dis `"{error:Option {bf:`replace'} and {bf:`append'} cannot exist at the same time}"'
error 9999
} //不能同时输入 replace 和 append 选项
* 如果 lines 非空,提取其所选取的最大行号
if ustrregexm("`lines'","(\d+)$") {
local max_num_of_select_lines = ustrregexs(1)
}
* 初始值
local linenum_read = 0 //读取文件时所进行到的行数
local linenum_write = 0 //写入文件时所进行到的行数
*---------------------------主程序-----------------------------
*file 命令的前端
tempname handle1 handle2
qui file open `handle1' using `"`using'"', read text
qui file open `handle2' using `"`saving'"', write text `replace' `append'
if "`append'" != "" {
file write `handle2' _n
} //append 选项如果存在,则在写入文件之前新起一行
*文章的读取与写入
file read `handle1' line
while r(eof) == 0 {
if "`lines'" == "" { //I.没有选定具体行号进行复制或附加的情况
local linenum_read = `linenum_read' + 1
local linenum_write = `linenum_write' + 1
file write `handle2' `"`line'"'
file read `handle1' line
if r(eof) == 0 {
file write `handle2' _n
} //如果下一行不是文本末端,才对上一行加上 newline
}
else { //II.有选定具体行号进行复制或附加的情况
local linenum_read = `linenum_read' + 1
if ustrregexm("`lines'","\b`linenum_read'\b") { //如果linenum_read与lines匹配上了,则执行复制或附加,并写入saving文件中
local linenum_write = `linenum_write' + 1
file write `handle2' `"`line'"' `=cond(`linenum_read'==`max_num_of_select_lines',"","_n")'
}
file read `handle1' line
}
}
if "`lines'" != "" {
if `max_num_of_select_lines' > `linenum_read' {
dis as error `"The selected maximum number of lines exceeds the maximum line of file {bf:"`saving'"}."'
error 9999
}
} //如果所选行数的最大值超过了文件本身的最大行数,则报错
*file 命令的末端
file close `handle1'
file close `handle2'
end