Last active
February 2, 2017 08:14
-
-
Save unau/2f75b7be86d6162ce17e393d87e06a2f to your computer and use it in GitHub Desktop.
awk 歴 23 年のおっさんが 2017 年に知った awk の小技 : 「プログラムファイルの分割」と「処理対象ファイルごとの分岐」 ref: http://qiita.com/unau/items/f98cae3de8722d05e9c2
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
FILENAME == "test1.csv" { | |
... | |
} |
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
FNR == 1 { | |
fname = gensub(/^.*\/([^\/]+)\.csv$/, "\\1", 1, FILENAME); | |
delete m; | |
m[fname] = 1; | |
} | |
m["test1"] { | |
# test1.csv に関する処理 | |
} | |
m["test2"] { | |
# test2.csv に関する処理 | |
} |
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
[takeyuki@jupiter ~]$ `(echo -n "awk "; ls -1 test*.awk | while read f; do echo -n "-f $f "; done; echo /var/test/*.csv)` |
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
m["test1"] && FNR < 3 { | |
stderr(sprintf("[%s:%03d] %s", fname, FNR, $0)); | |
} |
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
m["test2"] && FNR % 2 == 0 { | |
stderr(sprintf("[%s:%03d] %s", fname, FNR, $0)); | |
} |
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
FNR == 1 { | |
fname = gensub(/^.*\/([^\/]+)\.csv$/, "\\1", 1, FILENAME); | |
delete m; | |
m[fname] = 1; | |
} |
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
function stderr(msg) { | |
print msg > "/dev/stderr"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment