kapott

csvheader.sh

May 31st, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.64 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. ###
  4. # Prints the index number, followed by the value of the file's first
  5. # line. This means that CSV's with headers create a nice list and CSV
  6. # files without headers are easier to flesh out.
  7. ###
  8.  
  9. main() {
  10.   local file
  11.   file=$(check_args "$@")
  12.  
  13.   [[ $file == "1" ]] \
  14.       && echo "Could not find input file, or file does not exist" \
  15.       && exit 1
  16.  
  17.   csv_header=$(head -n 1 "${1}")
  18.   IFS=',' read -r -a array <<< "$csv_header"
  19.  
  20.   for index in "${!array[@]}"; do
  21.     printf "%s\t%s\n" "$index" "${array[index]}"
  22.   done
  23. }
  24.  
  25.  
  26. check_args() {
  27.     [[ ! -z $1 ]] && [[ -f "$1" ]] && echo "0" || echo "1"
  28. }
  29.  
  30. main "$@"
Advertisement
Add Comment
Please, Sign In to add comment