Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • Please be careful: if your CSV file contains spaces and commas (for example foo,"bar, baz",bar - it has two cells: foo, bar, baz and bar) it isn't easy to parse (and change) with sed or awk.
    – uzsolt
    Commented Mar 12, 2017 at 17:49
  • Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2
    – Elham
    Commented Mar 12, 2017 at 20:24
  • @uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.
    – Elham
    Commented Mar 15, 2017 at 14:02
  • if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)
    – uzsolt
    Commented Mar 15, 2017 at 17:19