こんにちは、LinuCエバンジェリストこと、鯨井貴博@opensourcetechです。
今回は awkコマンドを使った文字列操作を紹介します。
実は今までは、grepやらcutやら 複数のコマンドをパイプで連結させつつ使っていたのですが、awk使ってみると多機能で便利ですね。
今回は、いくつかの利用方法を抜粋して紹介していますが、
マニュアル全体については以下のリンクなどから確認できます。
https://www.gnu.org/software/gawk/manual/gawk.html
文字列の一部分を切り出す(substr)
substr(文字列,取り取り出し開始位置,取り出し完了位置)を使います。
なお、取り出し完了位置の指定がない場合、文字列の最後まで自動選択されます。
bash-3.2$ echo 12345
12345
bash-3.2$ echo 12345 | awk '{print substr($0,1)}'
12345
bash-3.2$ echo 12345 | awk '{print substr($0,2)}'
2345
bash-3.2$ echo 12345 | awk '{print substr($0,3)}'
345
bash-3.2$ echo 12345 | awk '{print substr($0,4)}'
45
bash-3.2$ echo 12345 | awk '{print substr($0,5)}'
5
bash-3.2$ echo 12345 | awk '{print substr($0,6)}'
bash-3.2$ echo 12345 | awk '{print substr($0,1,2)}'
12
正規表現で検索
/正規表現/ という書き方で使います。
なお、正規表現の意味については、こちらなど参考にしてください。
bash-3.2$ cat /etc/ttys
#
# @(#)ttys 5.2 (Berkeley) 6/10/93
#
# name getty type status comments
#
#console "/usr/libexec/getty std.57600" vt100 on secure
console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow" vt100 on secure onoption="/usr/libexec/getty std.9600"
#tahoe's only
#remote "/usr/libexec/getty std.1200" pt on # diagnostics
# The tty.serial entry initializes the serial port (if any) for use as a
# terminal (enabling logons over serial). If marked secure, the serial
# port will allow root logons.
# To make the serial port available for outbound
# communications, the tty.serial entry should be turned off
# (set the 4th field to off).
tty.serial "/usr/libexec/getty serial.57600" vt100 off secure
# Fax reception is off by default, use the
# System Preferences panel to enable it.
fax "/usr/bin/fax answer" unknown off
# Hardwired lines are marked off, by default, so getty(8)
# is quiet when they don't exist.
tty[00-07] "/usr/libexec/getty std.9600" unknown off secure
# pseudo-ttys
ttyp[0x0-0xf] none network slot=36
ttyq[0x0-0xf] none network
ttyr[0x0-0xf] none network
ttys[0x0-0xf] none network
ttyt[0x0-0xf] none network
ttyu[0x0-0xf] none network
ttyv[0x0-0xf] none network
ttyw[0x0-0xf] none network
# cloning ptys
ttys[000-999] none network
bash-3.2$ cat /etc/ttys | awk '/^#'
awk: non-terminated regular expression ^#... at source line 1
context is
>>> <<<
bash-3.2$ cat /etc/ttys | awk '/^#/ {print}'
#
# @(#)ttys 5.2 (Berkeley) 6/10/93
#
# name getty type status comments
#
#console "/usr/libexec/getty std.57600" vt100 on secure
#tahoe's only
#remote "/usr/libexec/getty std.1200" pt on # diagnostics
# The tty.serial entry initializes the serial port (if any) for use as a
# terminal (enabling logons over serial). If marked secure, the serial
# port will allow root logons.
# To make the serial port available for outbound
# communications, the tty.serial entry should be turned off
# (set the 4th field to off).
# Fax reception is off by default, use the
# System Preferences panel to enable it.
# Hardwired lines are marked off, by default, so getty(8)
# is quiet when they don't exist.
# pseudo-ttys
# cloning ptys
bash-3.2$ cat /etc/ttys | awk '/network$/ {print}'
ttyq[0x0-0xf] none network
ttyr[0x0-0xf] none network
ttys[0x0-0xf] none network
ttyt[0x0-0xf] none network
ttyu[0x0-0xf] none network
ttyv[0x0-0xf] none network
ttyw[0x0-0xf] none network
ttys[000-999] none network
bash-3.2$ cat /etc/ttys | awk '/tty./ {print}'
# @(#)ttys 5.2 (Berkeley) 6/10/93
# name getty type status comments
#console "/usr/libexec/getty std.57600" vt100 on secure
console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow" vt100 on secure onoption="/usr/libexec/getty std.9600"
#remote "/usr/libexec/getty std.1200" pt on # diagnostics
# The tty.serial entry initializes the serial port (if any) for use as a
# communications, the tty.serial entry should be turned off
tty.serial "/usr/libexec/getty serial.57600" vt100 off secure
# Hardwired lines are marked off, by default, so getty(8)
tty[00-07] "/usr/libexec/getty std.9600" unknown off secure
# pseudo-ttys
ttyp[0x0-0xf] none network slot=36
ttyq[0x0-0xf] none network
ttyr[0x0-0xf] none network
ttys[0x0-0xf] none network
ttyt[0x0-0xf] none network
ttyu[0x0-0xf] none network
ttyv[0x0-0xf] none network
ttyw[0x0-0xf] none network
ttys[000-999] none network
bash-3.2$ cat /etc/ttys | awk '/[x-z]/ {print}'
# @(#)ttys 5.2 (Berkeley) 6/10/93
# name getty type status comments
#console "/usr/libexec/getty std.57600" vt100 on secure
console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow" vt100 on secure onoption="/usr/libexec/getty std.9600"
#tahoe's only
#remote "/usr/libexec/getty std.1200" pt on # diagnostics
# The tty.serial entry initializes the serial port (if any) for use as a
# communications, the tty.serial entry should be turned off
tty.serial "/usr/libexec/getty serial.57600" vt100 off secure
# Fax reception is off by default, use the
# System Preferences panel to enable it.
fax "/usr/bin/fax answer" unknown off
# Hardwired lines are marked off, by default, so getty(8)
# is quiet when they don't exist.
tty[00-07] "/usr/libexec/getty std.9600" unknown off secure
# pseudo-ttys
ttyp[0x0-0xf] none network slot=36
ttyq[0x0-0xf] none network
ttyr[0x0-0xf] none network
ttys[0x0-0xf] none network
ttyt[0x0-0xf] none network
ttyu[0x0-0xf] none network
ttyv[0x0-0xf] none network
ttyw[0x0-0xf] none network
# cloning ptys
ttys[000-999] none network
bash-3.2$ cat /etc/ttys | awk '/[xyz]/ {print}'
# @(#)ttys 5.2 (Berkeley) 6/10/93
# name getty type status comments
#console "/usr/libexec/getty std.57600" vt100 on secure
console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow" vt100 on secure onoption="/usr/libexec/getty std.9600"
#tahoe's only
#remote "/usr/libexec/getty std.1200" pt on # diagnostics
# The tty.serial entry initializes the serial port (if any) for use as a
# communications, the tty.serial entry should be turned off
tty.serial "/usr/libexec/getty serial.57600" vt100 off secure
# Fax reception is off by default, use the
# System Preferences panel to enable it.
fax "/usr/bin/fax answer" unknown off
# Hardwired lines are marked off, by default, so getty(8)
# is quiet when they don't exist.
tty[00-07] "/usr/libexec/getty std.9600" unknown off secure
# pseudo-ttys
ttyp[0x0-0xf] none network slot=36
ttyq[0x0-0xf] none network
ttyr[0x0-0xf] none network
ttys[0x0-0xf] none network
ttyt[0x0-0xf] none network
ttyu[0x0-0xf] none network
ttyv[0x0-0xf] none network
ttyw[0x0-0xf] none network
# cloning ptys
ttys[000-999] none network
bash-3.2$ cat /etc/ttys | awk '/[^xyz]/ {print}'
#
# @(#)ttys 5.2 (Berkeley) 6/10/93
#
# name getty type status comments
#
#console "/usr/libexec/getty std.57600" vt100 on secure
console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow" vt100 on secure onoption="/usr/libexec/getty std.9600"
#tahoe's only
#remote "/usr/libexec/getty std.1200" pt on # diagnostics
# The tty.serial entry initializes the serial port (if any) for use as a
# terminal (enabling logons over serial). If marked secure, the serial
# port will allow root logons.
# To make the serial port available for outbound
# communications, the tty.serial entry should be turned off
# (set the 4th field to off).
tty.serial "/usr/libexec/getty serial.57600" vt100 off secure
# Fax reception is off by default, use the
# System Preferences panel to enable it.
fax "/usr/bin/fax answer" unknown off
# Hardwired lines are marked off, by default, so getty(8)
# is quiet when they don't exist.
tty[00-07] "/usr/libexec/getty std.9600" unknown off secure
# pseudo-ttys
ttyp[0x0-0xf] none network slot=36
ttyq[0x0-0xf] none network
ttyr[0x0-0xf] none network
ttys[0x0-0xf] none network
ttyt[0x0-0xf] none network
ttyu[0x0-0xf] none network
ttyv[0x0-0xf] none network
ttyw[0x0-0xf] none network
# cloning ptys
ttys[000-999] none network
文字列の長さを取得(length)
lengthの後に文字列指定を行います。
なお、文字列指定をしなかった場合は、$0が利用されます。
12345
bash-3.2$ echo 12345 | awk '{print length($0)}'
5
bash-3.2$ echo 12345 | awk '{print length}'
5
指定文字が何文字目にあるか(index)
indexは、該当文字列・なんという文字の位置かを指定します。
3
bash-3.2$ echo 23456 | awk '{print index($0,3)}'
2
小文字/大文字に変換(tolower/toupper)
これは、見たままですね。
abcde
bash-3.2$ echo abcde | awk '{print toupper($0)}'
ABCDE
正規表現に一致した位置を返してくれる(match)
1
bash-3.2$ echo abcde | awk '{print match($0,"b")}'
2
bash-3.2$ echo abcde | awk '{print match($0,"c")}'
3
bash-3.2$ echo abcde | awk '{print match($0,"d")}'
4
bash-3.2$ echo abcde | awk '{print match($0,"e")}'
5
bash-3.2$ echo abcde | awk '{print match($0,"e$")}'
5
bash-3.2$ echo abcde | awk '{print match($0,"^a")}'
1
bash-3.2$ echo abcde | awk '{print match($0,"^A")}'
0
bash-3.2$ echo abcde | awk '{print match($0,"E$")}'
0