1#!/bin/bash
2
3# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
4# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
5# Copyright 2013  Kyungwan.Han <asura321@gmail.com>
6
7[ -f testing.sh ] && . testing.sh
8
9#testing "name" "command" "result" "infile" "stdin"
10
11# Creating test file for testing cut
12echo "one:two:three:four:five:six:seven
13alpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:mu
14the quick brown fox jumps over the lazy dog" >abc.txt
15
16testing "-b a,a,a" "cut -b 3,3,3 abc.txt" "e\np\ne\n" "" ""
17testing "-b overlaps" "cut -b 1-3,2-5,7-9,9-10 abc.txt" \
18  "one:to:th\nalphabeta\nthe qick \n" "" ""
19testing "-b encapsulated" "cut -b 3-8,4-6 abc.txt" "e:two:\npha:be\ne quic\n" \
20  "" ""
21testing "-bO overlaps" \
22  "cut --output-delimiter ' ' -b 1-3,2-5,7-9,9-10 abc.txt" \
23  "one:t o:th\nalpha beta\nthe q ick \n" "" ""
24testing "high-low error" "cut -b 8-3 abc.txt 2>/dev/null || echo err" "err\n" \
25  "" ""
26
27testing "-c a-b" "cut -c 4-10 abc.txt" ":two:th\nha:beta\n quick \n" "" ""
28testing "-c a-" "cut -c 41- abc.txt" "\ntheta:iota:kappa:lambda:mu\ndog\n" "" ""
29testing "-c -b" "cut -c -39 abc.txt" \
30  "one:two:three:four:five:six:seven\nalpha:beta:gamma:delta:epsilon:zeta:eta\nthe quick brown fox jumps over the lazy\n" \
31  "" ""
32testing "-c a" "cut -c 40 abc.txt" "\n:\n \n" "" ""
33testing "-c a,b-c,d" "cut -c 3,5-7,10 abc.txt" "etwoh\npa:ba\nequi \n" "" ""
34SKIP_HOST=1 testing "-c japan.txt" 'cut -c 3-6,9-12 "$FILES/utf8/japan.txt"' \
35  "ガラスをられます\n" "" ""
36
37# substitute for awk
38SKIP_HOST=1 testcmd "-DF" "-DF 2,7,5" \
39  "said and your\nare\nis demand. supply\nforecast :\nyou you better,\n\nEm: Took hate\n" "" \
40"Bother, said Pooh. It's your husband, and he has a gun.
41Cheerios are donut seeds.
42Talk is cheap because supply exceeds demand.
43Weather forecast for tonight : dark.
44Apple: you can buy better, but you can't pay more.
45Subcalifragilisticexpialidocious.
46Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy."
47
48testcmd "empty field" "-d ':' -f 1-3" "a::b\n" "" "a::b\n"
49testcmd "empty field 2" "-d ':' -f 3-5" "b::c\n" "" "a::b::c:d\n"
50
51testing "-f a-" "cut -d ':' -f 5- abc.txt" "five:six:seven\nepsilon:zeta:eta:theta:iota:kappa:lambda:mu\nthe quick brown fox jumps over the lazy dog\n" "" ""
52
53testing "show whole line with no delim" "cut -d ' ' -f 3 abc.txt" \
54	"one:two:three:four:five:six:seven\nalpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:mu\nbrown\n" "" ""
55
56testing "with echo, -c (a-b)" "echo 'ref_categorie=test' | cut -c 1-15 " "ref_categorie=t\n" "" ""
57testing "with echo, -c (a)" "echo 'ref_categorie=test' | cut -c 14" "=\n" "" ""
58
59# Modifying abc.txt data as per new testcase
60echo "abcdefghijklmnopqrstuvwxyz" >abc.txt
61
62testing "with -c (a,b,c)" "cut -c 4,5,20 abc.txt" "det\n" "" ""
63
64testing "with -b (a,b,c)" "cut -b 4,5,20 abc.txt" "det\n" "" ""
65
66# Modifying abc.txt data as per testcase
67echo "406378:Sales:Itorre:Jan
68031762:Marketing:Nasium:Jim
69636496:Research:Ancholie:Mel
70396082:Sales:Jucacion:Ed" >abc.txt
71
72testing "with -d -f(:) -s" "cut -d: -f3 -s abc.txt" "Itorre\nNasium\nAncholie\nJucacion\n" "" ""
73
74testing "with -d -f( ) -s" "cut -d' ' -f3 -s abc.txt && echo yes" "yes\n" "" ""
75
76testing "with -d -f(a) -s" "cut -da -f3 -s abc.txt" "n\nsium:Jim\n\ncion:Ed\n" "" ""
77
78testing "with -d -f(a) -s -n" "cut -da -f3 -s -n abc.txt" "n\nsium:Jim\n\ncion:Ed\n" "" ""
79
80# Removing abc.txt file for cleanup purpose
81rm abc.txt
82