오랜만에 포스팅을 합니다.
뭐, 그냥 종종 사용하는 건데 맨날 까먹어서 계속 구글링하게 만드는 vi에서 문자열 치환하기에 대한 내용입니다.
*nix 환경에서 작업을 하게되면 설정 파일을 수정할 일이 많이 생깁니다.
이때 파일 다운로드 경로나, 이런 저런 경로들을 config 파일 안에 설정해놓고 사용하기도 하는데, 이때 경로 개수가 많고 앞부분의 경로는 동일한 것들이 많을 수 있습니다.
예를들어, /aaa/bbb/myapp/conf/config.properties 파일에 다음과 같은 내용들이 있을 수 있죠.
.
.
myapp.root=/aaa/bbb/myapp
myapp.log=/aaa/bbb/myapp/log
myapp.modules=/aaa/bbb/myapp/modules
.
.
저런식으로 경로가 저장이 되어있는데, 앞부분의 절대경로를 다른 경로로 변경해주고 싶을 때가 있습니다. 파일 사이즈가 작고 4~5개 정도면 하나하나 수정을 해줘도 되겠지만, 파일 사이즈가 크고 경로 설정하는 부분이 여기저기 흩어져 있으면 일일이 찾아서 수정하기가 쉽지 않습니다.
이럴 때 사용하라고 vi에서는 강력한 치환 기능을 제공하고있습니다.
%s/치환하려는문자열정규식/결과문자열정규식/옵션
vi로 문서를 열어놓은 상태에서 :을 입력하여 명령어를 입력할 수 있도록 한 뒤 위 형식에 맞게 문자열 및 옵션을 입력해주면 파일 전체에서 특정 문자열을 찾아서 결과문자열로 변경할 수 있습니다.
위 예를들어 /aaa/bbb/myapp을 /ccc/ddd/app 으로 변경을 하고 싶다면
%s/\/aaa\/bbb\/myapp/\/ccc\/ddd\/app/g
라고 입력해주면 전체 파일의 내용을 검색하여 /aaa/bbb/myapp 문자열을 찾은 뒤 /ccc/ddd/app 으로 치환해줍니다.
명령어를 살펴보면 다음과 같습니다.
%s : 문서 전체
g : 한줄에 동일 패턴이 여러개 존재하는 경우 모두 적용
i : 대소문자 구분 안함
이상으로 간만의 포스팅을 마칩니다~
리눅스 서버 재부팅/재기동 시간 확인하기 (0) | 2017.11.10 |
---|---|
[리눅스] war/jar 파일에 새 파일 추가 또는 변경 하기 (0) | 2017.11.09 |
리눅스에서 명령어 히스토리 조회 할 때 날짜 및 시간 함께 표시하기 (0) | 2017.11.02 |
[리눅스] find 와 wc를 이용한 하위 디렉토리 모든 파일 갯수 구하기 (0) | 2015.10.14 |
[리눅스] 정규표현식을 이용한 파일 삭제 (0) | 2015.10.14 |
SQL 커맨드라인에다가 2줄짜리 쿼리문을 한참 치고서 실행했더니 오타가 나서 다시 쳐야한다면???
OMG !!!!!!!!!!
이런 경우를 대비해서 오라클은 SQL 에디터를 제공해준다. 그런데 기본적으로 제공해주는게 뭔진 몰라도 불편하다.
좀더 편하게 하자면 우리가 유닉스 환경에서 자주 사용하는 vi 에디터를 SQL 기본 에디터로 설정해서 사용할 수 있는데
이 기본 에디터는 _editor 라는 변수안에 들어가있다.
SQL > define _editor = vi
위 명령어를 실행하면 다음부터는 ed라는 명령어를 이용해서 방금 쳤던 명령어를 vi에디터 모드에서 다시 보여준다.
짱이다.
[Oracle/SQL] instr, substr 을 이용해서 스트링 잘라내기 (0) | 2015.04.20 |
---|---|
[오라클/oracle] character set 변경 ( Changing Character Set ) (0) | 2015.04.20 |
[오라클/oracle] 내(현재) 계정 권한 조회하기 (0) | 2015.04.20 |
[Oracle/SQL] merge into (0) | 2015.04.20 |
[Oracle/SQL] 특정 사용자의 권한 조회 (0) | 2015.04.20 |
Working with files | |
Vim command | Action |
:e filename | Open a new file. You can use the Tab key for automatic file name completion, just like at the shell command prompt. |
:w filename | Save changes to a file. If you don't specify a file name, Vim saves as the file name you were editing. For saving the file under a different name, specify the file name. |
:q | Quit Vim. If you have unsaved changes, Vim refuses to exit. |
:q! | Exit Vim without saving changes. |
:wq | Write the file and exit. |
:x | Almost the same as :wq, write the file and exit if you've made changes to the file. If you haven't made any changes to the file, Vim exits without writing the file. |
Moving around in the file | |
These Vim commands and keys work both in command mode and visual mode. | |
Vim command | Action |
j or Up Arrow | Move the cursor up one line. |
k or Down Arrow | Down one line. |
h or Left Arrow | Left one character. |
l or Right Arrow | Right one character. |
e | To the end of a word. |
E | To the end of a whitespace-delimited word. |
b | To the beginning of a word. |
B | To the beginning of a whitespace-delimited word. |
0 | To the beginning of a line. |
^ | To the first non-whitespace character of a line. |
$ | To the end of a line. |
H | To the first line of the screen. |
M | To the middle line of the screen. |
L | To the the last line of the screen. |
:n | Jump to line number n. For example, to jump to line 42, you'd type :42 |
Inserting and overwriting text | |
Vim command | Action |
i | Insert before cursor. |
I | Insert to the start of the current line. |
a | Append after cursor. |
A | Append to the end of the current line. |
o | Open a new line below and insert. |
O | Open a new line above and insert. |
C | Change the rest of the current line. |
r | Overwrite one character. After overwriting the single character, go back to command mode. |
R | Enter insert mode but replace characters rather than inserting. |
The ESC key | Exit insert/overwrite mode and go back to command mode. |
Deleting text | |
Vim command | Action |
x | Delete characters under the cursor. |
X | Delete characters before the cursor. |
dd or :d | Delete the current line. |
Entering visual mode | |
Vim command | Action |
v | Start highlighting characters. Use the normal movement keys and commands to select text for highlighting. |
V | Start highlighting lines. |
The ESC key | Exit visual mode and return to command mode. |
Editing blocks of text | |
Note: the Vim commands marked with (V) work in visual mode, when you've selected some text. The other commands work in the command mode, when you haven't selected any text. | |
Vim command | Action |
~ | Change the case of characters. This works both in visual and command mode. In visual mode, change the case of highlighted characters. In command mode, change the case of the character uder cursor. |
> (V) | Shift right (indent). |
< (V) | Shift left (de-indent). |
c (V) | Change the highlighted text. |
y (V) | Yank the highlighted text. In Windows terms, "copy the selected text to clipboard." |
d (V) | Delete the highlighted text. In Windows terms, "cut the selected text to clipboard." |
yy or :y or Y | Yank the current line. You don't need to highlight it first. |
dd or :d | Delete the current line. Again, you don't need to highlight it first. |
p | Put the text you yanked or deleted. In Windows terms, "paste the contents of the clipboard". Put characters after the cursor. Put lines below the current line. |
P | Put characters before the cursor. Put lines above the current line. |
Undo and redo | |
Vim command | Action |
u | Undo the last action. |
U | Undo all the latest changes that were made to the current line. |
Ctrl + r | Redo. |
Search | |
Vim command | Action |
/pattern | Search the file for pattern. |
n | Scan for next search match in the same direction. |
N | Scan for next search match but opposite direction. |
Replace | |
Vim command | Action |
:rs/foo/bar/a | Substitute foo with bar. r determines the range and a determines the arguments. |
The range (r) can be | |
nothing | Work on current line only. |
number | Work on the line whose number you give. |
% | The whole file. |
Arguments (a) can be | |
g | Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line. |
i | Ignore case for the search pattern. |
I | Don't ignore case. |
c | Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches ("Yes to all"), and q to quit substitution. |
Examples | |
:452s/foo/bar/ | Replace the first occurrence of the word foo with bar on line number 452. |
:s/foo/bar/g | Replace every occurrence of the word foo with bar on current line. |
:%s/foo/bar/g | Replace every occurrence of the word foo with bar in the whole file. |
:%s/foo/bar/gi | The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on. |
:%s/foo/bar/gc | Confirm every substitution. |
:%s/foo/bar/c | For each line on the file, replace the first occurrence of foo with bar and confirm every substitution. |
Reference:
리눅스 서버의 플랫폼 확인하기 ( 32bit? 64bit ? ) (0) | 2015.06.09 |
---|---|
crond를 이용한 서버에서 원격지로 자동백업하기 (0) | 2015.04.20 |
.bashrc 와 .bash_profile의 차이점 (0) | 2015.04.20 |
su 와 su - 의 차이점?? ( Differences between su and su - ) (0) | 2015.04.20 |
FTP를 이용한 binary 데이타 백업 (0) | 2015.04.20 |