Parsing the first column of a csv file to a new file

前端 未结 4 926
礼貌的吻别
礼貌的吻别 2020-12-14 14:39

Operating System: OSX Method: From the command line, so using sed, cut, gawk, although preferably no installing modules.

Essentially I am trying to take the first c

4条回答
  •  情歌与酒
    2020-12-14 15:21

    Your last option works perfectly for me:

    $ cat > in.csv  # Then pasted the example input followed by Ctrl+D:
    EXAMPLEfoo,60,6
    EXAMPLEbar,30,6
    EXAMPLE1,60,3
    EXAMPLE2,120,6
    EXAMPLE3,60,6
    EXAMPLE4,30,6
    [Ctrl+D]
    $ cat in.csv | cut -d, -f1
    EXAMPLEfoo
    EXAMPLEbar
    EXAMPLE1
    EXAMPLE2
    EXAMPLE3
    EXAMPLE4
    

    Maybe line endings are biting you here? If the file has DOS-style or even old-Mac-style line endings, this might cause strange behaviour. Try running file in.csv and see what it comes up with.

    $ file in.unix.csv
    in.unix.csv: ASCII text
    $ file in.dos.csv
    in.dos.csv: ASCII text, with CRLF line terminators
    

    If the latter is your situation, use the dos2unix tool to convert the file.

    Edit: On OS X, it seems flip is what you want.

提交回复
热议问题