- 论坛徽章:
- 145
|
回复 1# rdcwayx
try this way if you didn't have awk 4.0
$ awk -f csv.awk FILE
NF = 8
modify for csv
NF = 7
$1 = <Robbins>
$2 = <Arnold>
$3 = <"1234 A Pretty Street, NE">
$4 = <MyTown>
$5 = <MyState>
$6 = <12345-6789>
$7 = <USA>
$ cat csv.awk
BEGIN{
FS=","
}
{
print "NF = ", NF
c = 0
for (n = 1; n <= NF; n++) {
if(q == 1){
if($n~/"$/){
#if(sub(/"$/,"",$n)){ # for removed the "
q = 0
}
$c = $c FS $n
continue
}
if($n~/^"/){
#if(sub(/^"/,"",$n)){ # for removed the "
q = 1
$(++c) = $n
continue
}
$(++c) = $n
}
NF = c
print "modify for csv"
print "NF = ", NF
for (n = 1; n <= NF; n++) {
printf("$%d = <%s>\n", n, $n)
}
}
|
|