help subinfile

Title

subinfile -- subinfile is intended for use by programmers who want to perform byte-based substitution for the contents of a text-formatted file.

Syntax

subinfile filesource, [options]

filesource must be the path and file name, whereas the file must be a text-formatted one, no matter the extention name.

Description

subinfile find the occorance of a strings and replace it with another.

subinfile requires Stata version 14 or higher.

Options for subinfile

index(string) specifies the line which contains it will be kept. Those lines without the key string specified by index() option will be dropped.

indexregex specifies that the contents you specify in index() is to be interpreted as a regular expression.

drop(string) specifies the line which contains it will be dropped. Those lines without the key string specified by drop() option will be kept.

dropregex specifies that the contents you specify in drop() is to be interpreted as a regular expression.

from(string1 [string2...]) and to(string1 [string2...]) specifies the strings which are to be replaced whereas the to() option specifies the new strings which
will be used to replace the old strings.

fromregex specifies that the contents you specify in from() is to be interpreted as a regular expression.

dropblank drops the blank line.

save(string) specifies the path and the file name to be saved. If you do not sepcify the format of the file,
it will be saved as .txt by default.

replace permits save to overwrite an existing file which is not read-only. If you do not specify the option save(string), the original file
will be replaced.

append permits save to append an existing file which is not read-only. If you do not specify the option save(string), the original file
will be appended.

order(string) Sets the order in which the options are executed. You can only specify index, drop, from, dropblank in order()

If you do not sepcify the option order(), the option index(string) will be executed first, then drop(string) and
from(string), and dropempty at last.

Example

Use command file to creat a text file to test subinfile

`"clear"'

`"tempname temp"'

`"file open `temp' using D:\temp.txt, write text replace"'

`"file write `temp' "stata" _n"'

`"file write `temp' "abcdefg" _n"'

`"file write `temp' "123456789""'

`"file close `temp'"'

Use command subinfile to substitute "stata" to "STATA".

`"subinfile D:\temp.txt, from("stata") to("STATA") save(D:\temp1.txt)"'

Use command subinfile to keep the lines that contain "stata".

`"subinfile D:\temp.txt, index("stata") save(D:\temp2.txt)"'

Use command subinfile and regular expression to substitute "abcdefg" to "alphabetic".

`"subinfile D:\temp.txt, from("[a-z]{7}") to("alphabetic") fromregex save(D:\temp3.txt)"'

Use command subinfile and regular expression to delete "123456789".

`"subinfile D:\temp.txt, from("\d") fromregex save(D:\temp4.txt)"'

Use command subinfile and regular expression to substitute "stata" to "STATA" and "1234" to "asdf".

\temp5.txt") replace"'

Use command subinfile to execute all options without order().

`"subinfile temp.txt, index("[a-z]") indexregex drop("stata") from("[a-z]") to("X") fromregex save("temp6.txt") replace"'

Use command subinfile to execute all options with order().

`"subinfile temp.txt, index("[a-z]") indexregex drop("stata") from("[a-z]") to("X") fromregex order(index from drop) save("temp7.txt") replace"'

Use command subinfile to extract information from source code of website.

//ideas.repec.org/s/boc/bocode.html" "temp.txt", replace"'

`"subinfile temp.txt, index(`"<LI class=""') from("(<A HREF=.*?>)|(</A></B><BR><I>)" "(^\s+)|(\s+$)|(<.*?>)") fromregex to("!@#$%^&*()_+" "") replace"'

`"import delimited using temp.txt, clear delimiter("!@#$%^&*()_+", asstring)"'

Author

Chuntao LI

China Stata Club(爬虫俱乐部)

Wuhan, China

chtl@zuel.edu.cn

Yuan XUE

China Stata Club(爬虫俱乐部)

Wuhan, China

xueyuan19920310@163.com