"Vi search & replace"의 두 판 사이의 차이

32번째 줄: 32번째 줄:
</source>
</source>


===파일 내용 변경==
===파일 내용 변경===
* 숫자 111을 aaa로 변경
* 숫자 111을 aaa로 변경
<source lang='console'>
<source lang='console'>

2017년 11월 5일 (일) 21:20 판

1 개요

VI search & replace
  • 원하는 디렉토리 모든 파일의 내용에서 특정 패턴을 새로운 패턴으로 변경
  • 변경 전에 변경에 대한 확인을 진행함

2 코드

# How to use
# replace.sh pattern new_pattern

files=$(find . -type f -name '*.*' -and ! -name $0)
pattern=$1
new_pattern=$2

for file in $files
do
    vim -c "%s/$pattern/$new_pattern/gc" -c "wq" $file
done

3 예시

3.1 디렉토리 구성

  • /test/a/b 구조에 a.txt 파일을 a, b디렉토리에 각각 배치
john@zetawiki:/test$ tree a
a
├── a.txt
└── b
    └── a.txt

1 directory, 2 files

3.2 파일 내용 변경

  • 숫자 111을 aaa로 변경
john@zetawiki:/test$ sh replace.sh 111 aaa

4 같이 보기

5 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}