logo

utils-std

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

cut.sh (1572B)


  1. #!/bin/sh
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. WD="$(dirname "$0")/../"
  5. target="${WD}/cmd/cut"
  6. plans=15
  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 %s - %s\n' "$count $name" "$out"
  18. elif [ "$out" != "$exp" ]; then
  19. printf 'not ok %s - (%s != %s)\n' "$count $name" "$out" "$exp"
  20. else
  21. printf 'ok %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. # Example taken from POSIX cut(1)
  33. t --input='abcdefghi' 'chars:6,2,4-7,1' '-c 6,2,4-7,1' 'abdefg
  34. '
  35. t 'chars:11-' "-c 11- ${WD}/test-cmd/inputs/alnum" 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
  36. '
  37. t 'chars:-10' "-c -10 ${WD}/test-cmd/inputs/alnum" '0123456789
  38. '
  39. t --input='aéb' 'widechar' '-c2' 'é
  40. '
  41. fields='1 2 3 4
  42. A
  43. a b c'
  44. t --input="$fields" f2 '-f2' '2
  45. A
  46. b
  47. '
  48. t --input="$fields" s_f2 '-s -f2' '2
  49. b
  50. '
  51. t --input="$fields" f2- '-f2-' '2 3 4
  52. A
  53. b c
  54. '
  55. t --input="$fields" f-2 '-f-2' '1 2
  56. A
  57. a b
  58. '
  59. t --input="hello" 'hello_f1' '-f1' 'hello
  60. '
  61. t --input="hello" 'hello_f1_s' '-f1 -s'
  62. t_null_f2