需要一个shell脚本来将逗号定界符更改为管道定界符 [英] need a shell script to change the comma delimiter to a pipe delimeter
本文介绍了需要一个shell脚本来将逗号定界符更改为管道定界符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的输入看起来像是 $ 130.00, $ 2,200.00, $ 1,230.63
等等
我的问题是如何更改逗号分隔符到|分隔符,而不会消除实际输入中的逗号。
只是为了澄清此输入,它是一个包含40列9500行的csv文件。
我希望输出看起来像
My input looks like "$130.00","$2,200.00","$1,230.63"
and so on
My question is how can I go about changing the comma delimiter to a | delimiter without getting rid of the comma in the actual input.
Just to clarify this input is in a csv file with 40 columns and 9500 rows.
I want my output to look like
"$130.00"|"$2,200.00"|"$1,230.63"
推荐答案
要可靠地做到这一点,您必须使用状态来跟踪天气您是否在字符串中。下面的perl脚本应该可以工作:
To do this reliably, you have to use states to keep track of wether you are inside a string or not. The following perl script should work:
#!/usr/bin/perl -w
use strict;
use warnings;
my $state_outside_string = 0;
my $state_inside_string = 1;
my $state = $state_outside_string;
while (my $line = <>) {
my @chars = split(//,$line);
foreach my $char (@chars) {
if ($char eq '"') {
if ($state == $state_outside_string) {
$state = $state_inside_string;
} else {
$state = $state_outside_string;
}
} elsif ($char eq ',') {
if ($state == $state_outside_string) {
print '|';
next;
}
}
print $char;
}
}
这篇关于需要一个shell脚本来将逗号定界符更改为管道定界符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文