Data cleaning & handling 101: a few commands to handle and clean csv or tsv files
Duplicates
Print duplicates rows:
$ sort file.csv | uniq -d
For the following examples I assume email addresses are the fifth column of a csv file.
Print the column with email addresses only:
$ cat file.csv | cut -d ',' -f 5
Sort the output of cat to group duplicates:
$ cat file.csv | cut -d ',' -f 5 | sort
Print duplicated email addresses only:
$ cat file.csv | cut -d ',' -f 5 | sort | uniq -d
Print the count of duplicated email addresses:
$ cat file.csv | cut -d ',' -f 5 | sort | uniq -d | wc -l
Create a new file without duplicates:
$ awk '!seen[$0]++' file.csv > no_dup.csv
See https://yctct.com/data-remove-duplicated-rows for explanation.
Edit file names in Vim
$ sudo apt install moreutils
$ vidir
Concatenate files
$ cat foo.txt bar.txt > foobar.txt
Append foo.txt to bar.txt
$ cat foo.txt >> bar.txt
Remove columns from a tsv or a csv file
To remove columns 1, 3 and 5 from a tsv file:
$ cut --complement -f 1,3,5 file.txt > new_file.txt
and from a csv file:
$ cut --complement -d ',' -f 1,3,5 file.txt > new_file.txt
Also, with Visidata: press - to hide the columns you want to delete and save your file CTRL + s. The file will be saved without the hidden columns.
Print files you modified in the last 24 hours in the current directory
$ find . -mtime 0
Misc
If there are missing email addresses in, say, col. 9, you can create a new file without rows which column 9 is empty (they don’t necessarily have an email address, but they have something):
$ awk -F',' '$9{print $0}' file.csv > newfile.csv
Print rows which have an email address, say in column 9:
$ cat file.csv | cut -d ',' -f 9 | grep @ -vn
(Leaves out row which don’t have an email address in column 9; the regex is not robust obviously.)
Remove rows without email addresses:
$ grep @ file.csv > newfile.csv
Remove ^M from file.csv:
$ vim file.csv
:%s/^M//g
NB: CTRL + V + M to print ^M
Remove a tab followed by a double-quote:
$ vim file.tsv
:%s/^I"//gc
NB: CTRL + V + I to print ^I
Check whether two files are identical:
$ diff -s file1.csv file2.csv
will output, if files are identical:
Files file1.csv and file2.csv are identical
if not, it will output the diff.
Using Visidata
Download Visidata on Trisquel GNU/Linux:
$ sudo apt install visidata
Open file with Visidata:
$ vd file.csv
To edit a cell, press e, edit, then press Enter.
To delete a row, press d.
To undo , press Shift + u.
To expand a column so you see all the text, press _ to toggle between the 2 widths.
To delete a column press - to hide the column first and then save your file CTRL + s. The file will be saved without the hidden column.
To save a file, press CTRL + S, then press Enter.
To open multiple files with Visidata, use regular expressions matching the names of the files you want to open.
For example, if you have:
$ ls
file_1.csv
file_2.csv
file_3.csv
run:
$ vd file*
This command will open the three files.
To browse the next file, and see one file after the other press q.
personal computing command-line interface (cli) gnu linux trisquel shell literacy wiki data