Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Alternative to dos2unix #117

Open
duskeer opened this issue Sep 11, 2021 · 0 comments
Open

Alternative to dos2unix #117

duskeer opened this issue Sep 11, 2021 · 0 comments

Comments

@duskeer
Copy link

duskeer commented Sep 11, 2021

dos2unix() {
# Usage: dos2unix [FILE]
if (($#)); then
  dos2unix < "${1}"; return 0
fi
local now on none
while read -rN1 now; do
  case "${now}" in
    $'\r')
      on=1
      none=0
    ;;
    $'\n')
      printf -- '\n'
      none=0
    ;;
    *)
      printf -- '%s' "${now}"
    ;;
  esac
done
case "${on:-"0"}${none:-"1"}"  in
  10|01)
    printf -- '\n'
  ;;
esac
}
unix2dos() {
# Usage: unix2dos [FILE]
if (($#)); then
  unix2dos < "${1}"; return 0
fi
local now old
while read -rN1 now; do
  case "${now}" in
    $'\n'|$'\r')
      :
    ;;
    *)
      case "${old:-""}" in
        $'\n')
          printf -- '\r\n%s' "${now}"
        ;;
        *)
          printf -- '%s' "${now}"
        ;;
      esac
    ;;
  esac
  old="${now}"
done
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant