linux+awk忽略tab符号,awk 如何避免文本中出现特殊符号的影响?

纯 awk 的解决方案,parse_csv 函数通过第一个返回一个 awk 数组,包含分隔好的 CSV 字段,直接用下标取值使用就可以了。注意,awk 的数组下标从 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++;

}

}

}

}'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值