2010. 12. 7. 08:49

sort로 output 정렬하고 활용하고 binary file에 문자열 찾기


sort 사용하기

sort로 명령의 output의 결과나 file의 내용을 정렬할 수 있습니다. 이는 output을 순서대로 만들어 주는데 도와줍니다. 아래 예제는 모든 설치된 RPM package의 이름 목록을 구하고, 이름중 kernel이라는 것을 찾아 그 결과를 alphanumeric 순으로 정렬합니다. (순차/역)

$ dpkg-query -l | grep kernel | sort
ii  kerneloops-daemon                    0.12+git20090217-1ubuntu7                       kernel oops tracker
ii  libdrm2                              2.4.18-1ubuntu3                                 Userspace interface to kernel DRM services -
ii  libdrm-intel1                        2.4.18-1ubuntu3                                 Userspace interface to intel-specific kernel
ii  linux-firmware                       1.34                                            Firmware for Linux kernel drivers
ii  linux-generic                        2.6.32.21.22                                    Complete Generic Linux kernel
ii  linux-headers-2.6.32-21              2.6.32-21.32                                    Header files related to Linux kernel version
ii  linux-headers-2.6.32-21-generic      2.6.32-21.32                                    Linux kernel headers for version 2.6.32 on x
ii  linux-headers-generic                2.6.32.21.22                                    Generic Linux kernel headers
ii  linux-image-2.6.32-21-generic        2.6.32-21.32                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-generic                  2.6.32.21.22                                    Generic Linux kernel image
ii  module-init-tools                    3.11.1-2ubuntu1                                 tools for managing Linux kernel modules
ii  udev                                 151-12                                          rule-based device node and kernel event mana
$ dpkg-query -l | grep kernel | sort -r
ii  udev                                 151-12                                          rule-based device node and kernel event mana
ii  module-init-tools                    3.11.1-2ubuntu1                                 tools for managing Linux kernel modules
...



아래는 memory 사용량 순서로 process를 정렬합니다 (ps output의 네번째 field). -k option은 정렬에 사용될 key field를 지정하는데 사용됩니다. 4,4는 네번째 field, 그리고 단지 네번째 field만 key field로 사용하겠다는 뜻입니다.
$ ps auwx | sort -r -k 4,4
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       810  0.2  3.9  28128 20048 tty7     Ss+  15:30   0:36 /usr/bin/X...
1000      6421  0.1  3.7  45560 18848 ?        S    16:20   0:17 /usr/lib/v...
1000      6434  0.0  3.6  70388 18484 ?        S    16:20   0:03 nautilus
1000      6528  0.0  2.9  31464 15140 ?        S    16:20   0:00 python /us...
1000      6423  0.0  2.9  40620 15056 ?        S    16:20   0:01 gnome-panel..
1000      6503  0.0  2.6  32036 13436 ?        S    16:20   0:00 /usr/lib/g...
1000      6507  0.0  2.5  46956 12784 ?        S    16:20   0:00 /usr/lib/i...
1000      6505  0.0  2.5  47988 12988 ?        S    16:20   0:00 /usr/lib/i...
...

아래 명령은 load된 kernel 모듈을 증가하는 크기 순으로 정렬한 경우입니다. n 옵션은 두번째 field를 문자열이 아닌 순자로 다뤄라라는 뜻입니다.
$ lsmod | sort -k 2,2n
Module                  Size  Used by
ac97_bus                1002  1 snd_ac97_codec
softcursor              1189  1 bitblit
snd_seq_dummy           1338  0
tileblit                2031  1 fbcon
serio_raw               3978  0
snd_seq_midi            4557  0
bitblit                 4707  1 fbcon
ppdev                   5259  0
snd_seq_device          5700  5 snd_seq_dummy,snd_seq_oss,snd_seq_midi,snd_rawmidi,snd_seq
snd_seq_midi_event      6003  2 snd_seq_oss,snd_seq_midi
soundcore               6620  1 snd
lp                      7028  0
snd_page_alloc          7076  1 snd_pcm
...


strings 사용하기

binary file에 ASCII 문자를 찾고 싶을 때가 있습니다. 보통 이런 경우 실행 파일이 동작하는 방법을 학습할 때 사용되기도 합니다. 사람이 읽을 수 있는 ASCII 문장을 찾는데 strings 명령을 사용하면 됩니다. strings 명령은 binutils package에 포함되어 있으며, Ubuntu에 기본으로 포함되어 있습니다. 아래의 예가 있습니다.

$ strings /bin/ls | grep -i libc
libc.so.6
__libc_start_main
GLIBC_2.2
GLIBC_2.2.3
GLIBC_2.4
GLIBC_2.3
GLIBC_2.3.4
GLIBC_2.1
GLIBC_2.1.3
GLIBC_2.0
$ cat /bin/ls | strings
...
.jcr
.dynamic
.got
.got.plt
.data
.bss
.gnu_debuglink
$ strings /bin/ls
...
xstrtol.c
0 <= strtol_base && strtol_base <= 36
xstrtoul
xstrtoumax
(NULL)