保险丝两个csv文件

时间:2017-06-09 13:06:37

标签: bash csv export-to-csv

我试图使用BASH以这种方式融合两个csv文件。

files1.csv:

@RestController
@RequestMapping( value = "/api")
public class Api {

    @Autowired
    private PRepository pRepository;

    @RequestMapping(value = "/v1", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ...(){
         ...
    }

    // --> I dont want rewrite "/v1"
    @RequestMapping(value = "/home", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ...(){
         ...
    }

    // --> I dont want rewrite "/v1/home"
    @RequestMapping(value = "/contact", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ...(){
         ...
    }

    // --> I dont want rewrite "/v1"
    @RequestMapping(value = "/book", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ...(){
         ...
    }

    // Version 2.0
    @RequestMapping(value = "/v2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ...(){
         ...
    }

    // --> I dont want rewrite "/v2"
    @RequestMapping(value = "/home", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ...(){
         ...
    }

    // --> I dont want rewrite "/v2/home"
    @RequestMapping(value = "/contact", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ...(){
         ...
    }

    // --> I dont want rewrite "/v2"
    @RequestMapping(value = "/book", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ...(){
         ...
    }

}

file2.csv

Col1;Col2
a;b
b:c

result.csv

Col3;Col4
1;2
3;4

结果文件中的' 0只是空单元格。 我尝试使用粘贴命令,但它并没有按照我想要的方式融合。

Col1;Col2;Col3;Col4
a;b;0;0
b;c;0;0
0;0;1;2
0;0;3;4

有没有办法用BASH做到?

感谢。

1 个答案:

答案 0 :(得分:1)

一个在awk中:

$ awk -v OFS=";" '
FNR==1  { a[1]=a[1] (a[1]==""?"":OFS) $0; next }    # mind headers
FNR==NR { a[NR]=$0 OFS 0 OFS 0; next }              # hash file1
        { a[NR]=0 OFS 0 OFS $0 }                    # hash file2
END     { for(i=1;i<=NR;i++)if(i in a)print a[i] }  # output
' file1 file2
Col1;Col2;Col3;Col4
a;b;0;0
b:c;0;0
0;0;1;2
0;0;3;4