diff options
| author | typebrook <typebrook@gmail.com> | 2020-03-02 20:34:54 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-03-02 20:34:54 +0800 |
| commit | a2497077066d4e26268bdd071851482252e45020 (patch) | |
| tree | f8f9c868abda19dd85744db84367966abe3f944b /tools/csv.move_column | |
| parent | af7ce43c718cb6bc0d7aac4633a8113f9a540ec5 (diff) | |
| parent | 00b136155183ae522ca458e540a8cf29bf525e74 (diff) | |
Merge branch 'dev' of github.com:typebrook/settings into dev
Diffstat (limited to 'tools/csv.move_column')
| -rwxr-xr-x | tools/csv.move_column | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/tools/csv.move_column b/tools/csv.move_column new file mode 100755 index 0000000..a62701f --- /dev/null +++ b/tools/csv.move_column | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | |||
| 3 | # show each field with index in csv | ||
| 4 | echo -------------- > /dev/tty | ||
| 5 | head -1 < $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty | ||
| 6 | echo -------------- > /dev/tty | ||
| 7 | echo > /dev/tty | ||
| 8 | |||
| 9 | # get index of lon/lat column | ||
| 10 | read -p "Move which column? " origin_col | ||
| 11 | read -p "To which index? " new_col | ||
| 12 | |||
| 13 | cat $1 | | ||
| 14 | # move lon and lat to the first and second column | ||
| 15 | awk -F',' -v OFS="," -v origin_th=$origin_col -v new_th=$new_col '\ | ||
| 16 | {for (i=1; i<= NF; i++) if (i == new_th) printf $origin_th OFS $i OFS; else if (i == origin_th); else printf $i OFS; print ""}\ | ||
| 17 | ' |\ | ||
| 18 | sed 's/,$//g' | ||