diff options
| author | typebrook <typebrook@gmail.com> | 2020-02-27 16:32:37 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-02-27 16:32:37 +0800 |
| commit | 00b136155183ae522ca458e540a8cf29bf525e74 (patch) | |
| tree | c6e9d20d5021c95afc7149456d57eb435d808150 /tools/csv.move_column | |
| parent | 3fa79eb14a4c0244fb2dc4a5b805b7cffaa63770 (diff) | |
update
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' | ||