Advertisement
cd62131

join files with mktemp

Mar 4th, 2019
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.49 KB | None | 0 0
  1. #!/bin/bash
  2. base='hoge_base.txt'
  3. files=('hoge2.txt' 'hoge3.txt')
  4. temp1=$(mktemp)
  5. temp2=$(mktemp)
  6. trap 'rm -f "${temp1}" "${temp2}"' ERR EXIT
  7. cp "${base}" "${temp1}"
  8. for ((i = 0; i < ${#files[@]}; ++i)); do
  9.   if ((i % 2 == 0)); then
  10.     join -o auto -a1 -e '---' "${temp1}" "${files[$i]}" >"${temp2}"
  11.   else
  12.     join -o auto -a1 -e '---' "${temp2}" "${files[$i]}" >"${temp1}"
  13.   fi
  14. done
  15. if ((${#files[@]} % 2 == 1)); then
  16.   cat "${temp2}"
  17. else
  18.   cat "${temp1}"
  19. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement