logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

cut.sh (1847B)


  1. #!/bin/sh
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. plans=18
  5. WD="$(dirname "$0")/../"
  6. target="${WD}/cmd/cut"
  7. . "${WD}/test-cmd/tap.sh"
  8. . "${WD}/test-cmd/init_env.sh"
  9. t_null_f2()
  10. {
  11. name="null_f2"
  12. exp="_2"
  13. count=$((count+1))
  14. out="$("$target" -d '' -f2 "${WD}/test-cmd/inputs/strings/length" 2>&1)"
  15. ret="$?"
  16. if [ "$?" != 0 ]; then
  17. printf 'not ok %d - %s %s\n' "$count" "$name" "$out"
  18. elif [ "$out" != "$exp" ]; then
  19. printf 'not ok %d - %s (%s != %s)\n' "$count" "$name" "$out" "$exp"
  20. else
  21. printf 'ok %d - %s\n' "$count" "$name"
  22. fi
  23. }
  24. t 'bytes:2-3,10-20,4,12' "-b 2-3,10-20,4,12 ${WD}/test-cmd/inputs/alnum" '1239ABCDEFGHIJ
  25. '
  26. t 'bytes:11-' "-b 11- ${WD}/test-cmd/inputs/alnum" 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
  27. '
  28. t 'bytes:-10' "-b -10 ${WD}/test-cmd/inputs/alnum" '0123456789
  29. '
  30. t --exit=1 'bytes:,' "-b , ${WD}/test-cmd/inputs/alnum" 'cut: error: Empty list element
  31. '
  32. # The cafés tests assumes a UTF-8 environment
  33. t --input='cafés' 'bytes:cafés[4]' '-b 4' 'Ã
  34. '
  35. # per POSIX high only decrements so here it gets dropped
  36. t --input='cafés' 'bytes:cafés[4]' '-b 4 -n' '
  37. '
  38. t --input='cafés' 'bytes:cafés[5]' '-b 5 -n' 'é
  39. '
  40. # Example taken from POSIX cut(1)
  41. t --input='abcdefghi' 'chars:6,2,4-7,1' '-c 6,2,4-7,1' 'abdefg
  42. '
  43. t 'chars:11-' "-c 11- ${WD}/test-cmd/inputs/alnum" 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
  44. '
  45. t 'chars:-10' "-c -10 ${WD}/test-cmd/inputs/alnum" '0123456789
  46. '
  47. t --input='aéb' 'widechar' '-c2' 'é
  48. '
  49. fields='1 2 3 4
  50. A
  51. a b c'
  52. t --input="$fields" f2 '-f2' '2
  53. A
  54. b
  55. '
  56. t --input="$fields" s_f2 '-s -f2' '2
  57. b
  58. '
  59. t --input="$fields" f2- '-f2-' '2 3 4
  60. A
  61. b c
  62. '
  63. t --input="$fields" f-2 '-f-2' '1 2
  64. A
  65. a b
  66. '
  67. t --input="hello" 'hello_f1' '-f1' 'hello
  68. '
  69. t --input="hello" 'hello_f1_s' '-f1 -s'
  70. t_null_f2