Last active
September 10, 2022 07:42
-
-
Save kin29/18349f0aec11df73325ab466f8621232 to your computer and use it in GitHub Desktop.
[shell]awkコマンド
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### awkコマンド | |
# テキストデータの加工や処理などができる。 | |
# 1が表示される | |
echo 1 2 | awk '{print $1}' | |
# 計算もできる | |
# 3(=1+2)が表示される | |
echo 1 2 | awk '{print $1+$2}' | |
# 「;(セミコロン)」で複数処理できる | |
echo 1 2 | awk '{x=$1+$2; print x}' | |
# seqコマンドと組み合わせる | |
seq 1 10 | awk '{x=$1+100; print x}' | |
## 条件に当てはまるものだけの出力 | |
# 私が生まれてからオリンピック開催年を出力 | |
seq 1993 2019 | awk '$1%4==0{print}' | |
# 私が生まれてからオリンピック開催回数を出力 | |
seq 1993 2019 | awk '$1%4==0{print}' | wc -l | |
#forループで九九の計算式を出力 | |
for a in `seq 1 9`;do for b in `seq 1 9`; do echo $a $b ; done ; done | awk '{print $1 "*" $2 "=" $1*$2}' | |
# awk.txtはファイルなので「|」はいらない | |
# awk '{print $1 * $2;}' dummy/awk.txtと同じ | |
# dummy/awk.txtの中身は「1 2<改行>3 4」のとき | |
awk '{print $1 + $2;}' < awk.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment