linux 에서 문자열 치환(replace) 하고 싶을 때는 sed 를 사용한다.

$ sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
      --debug
                 annotate program execution
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -b, --binary
                 open files in binary mode (CR+LFs are not processed specially)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use POSIX -E).
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
      --sandbox
                 operate in sandbox mode (disable e/r/w commands).
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.
use test_<suffix>;

create table tbl_test
(
);

위와 같은 create script 에서 <suffix> 만 바꿔서 돌리고 싶은 경우가 있다.

$ sed "s/<suffix>/01/g" test.txt
use test_01;

create table tbl_test
(
);

s/[기존 문자열]/[변경 후 문자열]/g 를 입력하면 test.txt 를 읽어 [기존 문자열] 을 [변경 후 문자열] 로 바꾸어 출력해 준다.

$ sed "s/<suffix>/01/g" -i test.txt

$ cat test.txt
use test_01;

create table tbl_test
(
);

-i 파라미터를 사용하면 입력 파일을 바로 바꾼다.

$ sed "s/<suffix>/01/g" -i.bak test.txt

$ ls test*
test.bat  test.txt  test.txt.bak

-i 뒤에 문자열을 입력하면 해당 확장자로 파일을 백업하고 치환 작업을 진행한다.

http://fart-it.sourceforge.net/

 

FART: Find And Replace Text

 

fart-it.sourceforge.net

참고로 윈도우즈 에서는 fart (Find And Replace Text) 라는 프로그램이 있다.

 

728x90

+ Recent posts