Parse a csv using awk and ignoring commas inside a field

前端 未结 7 1189
抹茶落季
抹茶落季 2020-11-29 04:36

I have a csv file where each row defines a room in a given building. Along with room, each row has a floor field. What I want to extract is all floors in all buildings. <

7条回答
  •  不知归路
    2020-11-29 04:51

    My workaround is to strip commas from the csv using:

    decommaize () {
      cat $1 | sed 's/"[^"]*"/"((&))"/g' | sed 's/\(\"((\"\)\([^",]*\)\(,\)\([^",]*\)\(\"))\"\)/"\2\4"/g' | sed 's/"(("/"/g' | sed 's/"))"/"/g' > $2
    }
    

    That is, first substitute opening quotes with "((" and closing quotes with "))", then substitute "(("whatever,whatever"))" with "whateverwhatever", then change all remaining instances of "((" and "))" back to ".

提交回复
热议问题