Batch Convert ASCII text or binary patterns in files
Syntax
freplace file_extension , from(oldpattern) [to(newpattern)]
where oldpattern and newpattern for ASCII characters are
"string" (include Chinese characters etc.) or string
string := [char[char[char[...]]]] char := regchar | code regchar := ASCII 32-91, 93-128, 161-255; excludes '\' code := \BS backslash \r carriage return \n newline \t tab \M Mac EOL, or \r \W Windows EOL, or \r\n \U Unix EOL, or \n \LQ left single quote, ` \RQ right single quote, ' \Q double quote, " \$ dollar sign, $ \###d 3-digit [0-9] decimal ASCII \##h 2-digit [0-9,A-F] hexadecimal ASCII
options Description ------------------------------------------------------------------------- * file_extension The file extension of files in current directory * from(oldpattern) find oldpattern to be replaced * to(newpattern) use newpattern to replace of from() -------------------------------------------------------------------------
Description
Attention !!! Before using freplace, you'd better back up your files!
freplace provides a batch process to replace the oldpattern of all file_extension type of files in current directory to the newpattern, which reads an file, searching for the oldpattern. Whenever a matching pattern is found, it is replaced with newpattern, and the file is arbitrarily overwritten! So, Before using freplace, you'd better back up your files!
freplace is based on the stata package of filefilter, and because of the buffering design of that package, arbitrarily large files can be converted quickly.
Examples
. freplace txt, from(\M) to(\W)
. freplace raw, f(\t) t(" ")
. freplace raw, f(\t)
. freplace log, f(\Q\W) t(\20h)
. freplace txt, f("US$") t("USD")
. freplace txt, f(1998) t(2001)
The ASCII table
+-----------------------------------------------------------------------+ | | we also | | | | write written | | | Category | Decimal Hex as as | Description | |-----------+----------------------------------+------------------------| | Control | 000 00 \0 NUL | null | | Codes | 001 01 ^A SOH | start of heading | | | 002 02 ^B STX | start of text | | | 003 03 ^C ETX | end of text | | | 004 04 ^D EOT | end of transmission | | | 005 05 ^E ENQ | enquiry | | | 006 06 ^F ACK | acknowledge | | | 007 07 ^G BEL | bell | | | 008 08 ^H BS | backspace | | | 009 09 \t HT ^I | horizontal tab | | | 010 0a \n LF ^J | line feed, newline | | | 011 0b ^K VT | vertical tabulation | | | 012 0c ^L FF | form feed | | | 013 0d \r CR ^M | carriage return | | | 014 0e ^N SO | shift out | | | 015 0f ^O SI | shift in | | | 016 10 ^P DLE | data link escape | | | 017 11 ^Q DC1 XON | device control 1 | | | 018 12 ^R DC2 | device control 2 | | | 019 13 ^S DC3 XOFF | device control 3 | | | 020 14 ^T DC4 | device control 4 | | | 021 15 ^U NAK | negative acknowledge | | | 022 16 ^V SYN | synchronous idle | | | 023 17 ^W ETB | end transmission block | | | 024 18 ^X CAN | cancel | | | 025 19 ^Y EM | end of medium | | | 026 1a ^Z SUB | substitute | | | 027 1b Esc ESC ^[ | escape | | | 028 1c 28 FS ^\ | file separator | | | 029 1d 29 GS ^] | group separator | | | 030 1e 30 RS ^^ | record separator | | | 031 1f 31 US ^_ | unit separator | |-----------+----------------------------------+------------------------| | ASCII | 032 20 blank SP | space | | printable | 033 21 ! | exclamation point | | | 034 22 " | quotation mark | | | 035 23 # | number sign | | | 036 24 $ | dollar sign | | | 037 25 % | percent sign | | | 038 26 & | ampersand | | | 039 27 ' | apostrophe | | | 040 28 ( | opening parenthesis | | | 041 29 ) | closing parenthesis | | | 042 2a ) | closing parenthesis | | | 043 2b + | plus | | | 044 2c , | comma | | | 045 2d - | hyphen | +-----------------------------------------------------------------------+
Saved results
freplace saves the following in r():
Scalars r(occurrences) total number of oldpattern found r(bytes_from) # of bytes represented by oldpattern r(bytes_to) # of bytes represented by newpattern
Acknowledgements
The codes to get directory information are traced back to Morten Andersen's dirlist.
For problems and suggestions
Author: Liu wei, The School of Sociology and Population Studies, Renmin University of China. Address: Zhongguancun Street No. 59, Haidian District, ZIP Code: 100872. E-mail: rucwz-stata@yahoo.cn
Also see
Other Commands I have written:
curvefit (if installed) ssc install curvefit (to install) deci (if installed) ssc install deci (to install) fdta (if installed) ssc install fdta (to install) ftrans (if installed) ssc install ftrans (to install) elife (if installed) ssc install elife (to install) ftree (if installed) ssc install ftree (to install) fren (if installed) ssc install fren (to install)