sed : non-interactive mode line editor
===============================
sed는 stream editor의 준말이다.
sed는 라인기반 스트림편집기이므로 명령어는 각 라인에 대해 순서대로 수행된다
--------------------------------------------------
(1)[주소-범위] /p
주어진 주소 범위 출력
(2)[주소-범위] /d
주어진 주소 범위 삭제
(3) s/pattern1/pattern2
처음나타나는 pattern1을 pattern2로 바꿈
(4) [주소-범위]/s/pattern1/pattern2
주소범위의 pattern1을 pattern2로 바꿈
(5) g
global, 모든 패턴
FOLLOWING IS FROM Bash-Prog-Intro-HOWTO-11.html
------------------------------------------------------------------------------------
This section was re-written by Kees (see thank to...)
Some of these command's almost contain complete programming languages. From those commands only the basics will be explained. For a more detailed description, have a closer look at the man pages of each command.
sed (stream editor)
Sed is a non-interactive editor. Instead of altering a file by moving the cursor on the screen, you use a script of editing instructions to sed, plus the name of the file to edit. You can also describe sed as a filter. Let's have a look at some examples:
$sed 's/to_be_replaced/replaced/g' /tmp/dummy
Sed replaces the string 'to_be_replaced' with the string 'replaced' and reads from the /tmp/dummy file. The result will be sent to stdout (normally the console) but you can also add '> capture' to the end of the line above so that sed sends the output to the file 'capture'.
$sed 12, 18d /tmp/dummy
Sed shows all lines except lines 12 to 18. The original file is not altered by this command.
awk (manipulation of datafiles, text retrieval and processing)
Many implementations of the AWK programming language exist (most known interpreters are GNU's gawk and 'new awk' mawk.) The principle is simple: AWK scans for a pattern, and for every matching pattern a action will be performed.
Again, I've created a dummy file containing the following lines:
"test123
test
tteesstt"
$awk '/test/ {print}' /tmp/dummy
test123
test
The pattern AWK looks for is 'test' and the action it performs when it found a line in the file /tmp/dummy with the string 'test' is 'print'.
$awk '/test/ {i=i+1} END {print i}' /tmp/dummy
3
When you're searching for many patterns, you should replace the text between the quotes with '-f file.awk' so you can put all patterns and actions in 'file.awk'.
grep (print lines matching a search pattern)
We've already seen quite a few grep commands in the previous chapters, that display the lines matching a pattern. But grep can do more.
$grep "look for this" /var/log/messages -c
12
The string "look for this" has been found 12 times in the file /var/log/messages.
[ok, this example was a fake, the /var/log/messages was tweaked :-)]
wc (counts lines, words and bytes)
In the following example, we see that the output is not what we expected. The dummy file, as used in this example, contains the following text: "bash introduction howto test file"
$wc --words --lines --bytes /tmp/dummy
2 5 34 /tmp/dummy
Wc doesn't care about the parameter order. Wc always prints them in a standard order, which is, as you can see: .
sort (sort lines of text files)
This time the dummy file contains the following text:
"b
c
a"
$sort /tmp/dummy
This is what the output looks like:
a
b
c
Commands shouldn't be that easy :-) bc (a calculator programming language)
Bc is accepting calculations from command line (input from file. not from redirector or pipe), but also from a user interface. The following demonstration shows some of the commands. Note that
I start bc using the -q parameter to avoid a welcome message.
$bc -q
1 == 5
0
0.05 == 0.05
1
5 != 5
0
2 ^ 8
256
sqrt(9)
3
while (i != 9) {
i = i + 1;
print i
}
123456789
quit
tput (initialize a terminal or query terminfo database)
A little demonstration of tput's capabilities:
$tput cup 10 4
The prompt appears at (y10,x4).
$tput reset
Clears screen and prompt appears at (y1,x1). Note that (y0,x0) is the upper left corner.
$tput cols
80
Shows the number of characters possible in x direction.
It it higly recommended to be familiarized with these programs (at least). There are tons of little programs that will let you do real magic on the command line.
---------------------------------------------
Arithmetic relational operators
---------------------------------------------
-lt (<)
-gt (>)
-le (<=)
-ge (>=)
-eq (==)
-ne (!=)
---------------------------------------------
String comparison operators
---------------------------------------------
(1) s1 = s2
(2) s1 != s2
(3) s1 < s2
(4) s1 > s2
(5) -n s1
(6) -z s1
(1) s1 matches s2
(2) s1 does not match s2
논리연산
!
-a AND
-o OR
- FOLLOWING IS FROM "Advanced Bash Scripting HOWTO"
----------------------------------------------------------------------------------
-e 존재하는 파일- -f 보통 파일(디렉토리나 디바이스 파일이 아님)
- -s 파일 크기가 0 이 아님
- -d 파일이 디렉토리
- -b 파일이 블럭 디바이스(플로피나 시디롬 등등)
- -c 파일이 문자 디바이스(키보드, 모뎀, 사운드 카드 등등)
- -p 파일이 파이프
- -h 파일이 심볼릭 링크
- -L 파일이 심볼릭 링크
- -S 파일이 소켓
- -t 파일 디스크립터가 터미널 디바이스와 연관이 있음. 스크립트의 표준입력([ -t 0 ])이나 표준출력([ -t 1 ])이 터미널인지 아닌지를 확인하는데 쓸 수 있습니다.
- -r 테스트를 돌리는 사용자가 읽기 퍼미션을 갖고 있음
- -w 테스트를 돌리는 사용자가 쓰기 퍼미션을 갖고 있음
- -x 테스트를 돌리는 사용자가 실행 퍼미션을 갖고 있음
- -g 파일이나 디렉토리에 set-group-id(sgid) 플래그가 세트되어 있음
- -f 보통 파일(디렉토리나 디바이스 파일이 아님)
-
디렉토리에 sgid 플래그가 세트되어 있다면 그 디렉토리에서 만들어지는 파일은 파일 생성자의 그룹이 아니라 그 디렉토리 소유자의 그룹에 속하게 됩니다. 이는 워킹그룹이 공유하는 디렉토리에서 유용하게 쓸 수 있습니다.
- -u 파일에 set-user-id(suid) 플래그가 세트되어 있음
-
root가 소유자인 어떤 실행 파일에 set-user-id 플래그가 세트되어 있다면 일반 사용자가 그 파일을 실행시키더라도 root 권한으로 실행됩니다. [1] suid 는 시스템 하드웨어에 접근할 필요가 있는 실행 파일(pppd나 cdrecord 같은)에 유용합니다. suid 플래그가 없다면 이런 실행 파일들은 일반 사용자들이 실행 시킬 수 없습니다.
-rwsr-xr-t 1 root 178236 Oct 2 2000 /usr/sbin/pppd
- -k 스티키 비트(sticky bit)가 세트
-
보통은 "스키키 비트"라고 알려져 있는 세이브-텍스트-모드(save-text-mode) 플래그는 특별한 형태의 파일 퍼미션입니다. 어떤 파일에 이 플래그가 세트되어 있다면 그 파일은 더 빠른 접근을 위해 캐쉬 메모리에 계속 남아 있습니다. [2] 만약에 디렉토리에 세트되어 있다면 쓰기 퍼미션을 제한합니다. 스티키 비트가 세트되어 있다면 파일이나 디렉토리 퍼미션에 t 가 붙어서 보입니다.
drwxrwxrwt 7 root 1024 May 19 21:26 tmp/
- -O 자신이 소유자임
- -G 그룹 아이디가 자신과 같음
- -N 마지막으로 읽힌 후에 변경 됐음
- f1 -nt f2 f1 파일이 f2 파일보다 최신임
- f1 -ot f2 f1 파일이 f2 파일보다 예전것임
- f1 -ef f2 f1 파일과 f2 파일이 같은 파일을 하드 링크하고 있음
- ! "not" -- 앞에서 나왔던 테스트의 의미와 반대(조건이 안 맞으면 참).
- -G 그룹 아이디가 자신과 같음