在shell中导致的字段分隔符

时间:2015-10-21 17:11:04

标签: sed

我有csv文件,如:

 Apartments, 891 , 413 ," 1,623 "," 2,415 "," 6,565 "," 2,916 ", -   , 436 , -   , 620 ," 1,651 "," 2,050 "," 1,200 "," 3,073 ", 89 , -   , 24 ," 3,189 ", -   ," 3,2    18 "," 1,768 ", 580 , -   ," 1,107 "," 9,298 "," 9,178 "," 7,326 "," 3,356 "," 4,181 "," 2,912 "," 8,669 "," 2,584 "," 3,835 ", 901 ," 6,703 "," 7,150 "," 3,938 ","     13,867 "," 2,233 "," 6,839 "," 4,754 "," 6,594 ", -   ," 2,582 "," 2,359 ", 624 , 416 , -   ," 1,362 ", 280 ," 1,548 ", 595 ," 1,744 ", -   , 481 ," 1,285 "," 3,07    7 "," 3,734 "," 2,339 "," 2,220 "," 1,053 ", 632 ," 8,898 ", 539 , -   ," 4,355 "

我希望所有文件都被|分开并且所有数字都应该是明确的。 并且没有,在一个数字。

我希望输出如下:

Apartments| 891 | 413 | 1623 | 2415 |6565 | 2916 | -  | 436 | -  | 620 |

1 个答案:

答案 0 :(得分:1)

在这里,使用命令可以轻松实现,如下所示;

sed -e 's/\(\"[^",]\+\),\([^",]*\)/\1\2/g' -e 's/\"//g' sample |tr ',' '|'

<强>解释

首先sed删除两个引号之间的逗号,然后tr命令用管道替换所有其余的逗号。

<强>输出:

$ sed -e 's/\(\"[^",]\+\),\([^",]*\)/\1\2/g' -e 's/\"//g' sample.csv |tr ',' '|'
Apartments| 891 | 413 | 1623 | 2415 | 6565 | 2916 | -   | 436 | -   | 620 | 1651 | 2050 | 1200 | 3073 | 89 | -   | 24 
    | 3189 | -   | 32    18 | 1768 | 580 | -   | 1107 | 9298 | 9178 | 7326 | 3356 | 4181 | 2912 | 8669 | 2584 | 3835 | 901
     | 6703 | 7150 | 3938 |     13867 | 2233 | 6839 | 4754 | 6594 | -   | 2582 | 2359 | 624 | 416 | -   | 1362 | 280 | 154
    8 | 595 | 1744 | -   | 481 | 1285 | 307    7 | 3734 | 2339 | 2220 | 1053 | 632 | 8898 | 539 | -   | 4355