Skip to content

Cat

cat 명령어는 텍스트파일 내용을 출력하거나 두 개 이상의 파일을 연결하여 출력할 수 있다.

Example

각 행에 번호를 붙여서 출력:

$ cat -b test.txt

빈 행에도 번호를 붙임:

$ cat -n test.txt

연속되는 2개이상의 빈 행을 한행으로 출력:

cat -s test.txt

Assign multi-line string

Examples of cat <<EOF syntax usage in Bash. 참고로 이러한 방법을 Here document라고 한다. 자세한 내용은 해당 항목 참조.

Assign multi-line string to a shell variable

$ sql=$(cat <<EOF
SELECT foo, bar FROM db
WHERE foo='baz'
EOF
)

The $sql variable now holds the new-line characters too. You can verify with echo -e "$sql"

Pass multi-line string to a file in Bash

$ cat <<EOF > print.sh
#!/bin/bash
echo \$PWD
echo $PWD
EOF

The print.sh file now contains:

#!/bin/bash
echo $PWD
echo /home/user

Pass multi-line string to a pipe in Bash

$ cat <<EOF | grep 'b' | tee b.txt
foo
bar
baz
EOF

The b.txt file contains bar and baz lines. The same output is printed to stdout.

환경변수 치환을 막고 싶다면

다음과 같이 사용하면 $temp가 환경변수로 치환된다.

$ cat << EOF > tee b.txt
$temp
EOF

이 현상을 막으려면 EOF를 작은 따옴표로 쿼우팅('EOF') 해야 한다.

$ cat << 'EOF' > tee b.txt
$temp
EOF

Text Processing Commands

텍스트 처리 명령어 목록

Text

Common

grep, sed, awk, cat, xargs, rev, tee, ack, tr, stdbuf

Diff

diff, patch

Cutting

cut, head, tail, less, more, split, watch

Sort

uniq, sort

Hash

base64, md5sum, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum

Clipboard

clip (Windows), pbcopy (Mac OSX), pbpaste (Mac OSX), xclip (Debian)

E.T.C

regexp, parallel

See also

Favorite site