linux+awk忽略tab符号,awk 如何避免文本中出现特殊符号的影响?
純 awk 的解決方案,parse_csv 函數(shù)通過第一個返回一個 awk 數(shù)組,包含分隔好的 CSV 字段,直接用下標取值使用就可以了。注意,awk 的數(shù)組下標從 1 開始。
# parse csv files to an awk array.
awk -F, '{
parse_csv(r);
# replace following line with your code.
print r[1], r[2], r[3], r[4];
} function parse_csv(r, _quote, _i, _n) {
_i = 1;
_quote = 0; # in a quoted string or not.
for (_n = 1; _n <= NF; _n++) {
if (_quote) {
# quote string meets its end.
if (substr($_n, length($_n), 1) == "\"") {
_quote = 0;
r[_i] = r[_i]","substr($_n, 1, length($_n) - 1);
_i++;
} else {
# concat quote string with ",".
r[_i] = r[_i]","$_n;
}
} else {
# it is a quote string.
if (substr($_n, 1, 1) == "\"") {
_quote = 1;
r[_i] = substr($_n, 2);
} else {
# copy content for normal string without quote.
r[_i] = $_n;
_i++;
}
}
}
}'
總結(jié)
以上是生活随笔為你收集整理的linux+awk忽略tab符号,awk 如何避免文本中出现特殊符号的影响?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 花呗欠4000要逾期了,真的会爆通讯录吗
- 下一篇: 微信能够提取公积金吗?