-
Notifications
You must be signed in to change notification settings - Fork 0
/
ersetze
executable file
·1933 lines (1778 loc) · 65.3 KB
/
ersetze
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#!/bin/bash
#
# @author Zeus Intuivo <[email protected]>
#
# ls | xargs -I {} sed -i -e 's@bash_crm_cli@bash_intuivo_cli@g' {}
# find * -type f | xargs -I {} sed -i -e 's@bash_crm_cli@bash_intuivo_cli@g' {}
#
THISSCRIPTNAME=`basename "$0"`
# bash shell script check input argument
# Bash: Detect pipe/file input in a shell script
# REF: https://gist.github.com/davejamesmiller/1966557
# How to detect whether input is from keyboard, a file, or another process.
# Useful for writing a script that can read from standard input, or prompt the
# user for input if there is none.
# Source: http://www.linuxquestions.org/questions/linux-software-2/bash-scripting-pipe-input-to-script-vs.-1-570945/
PIPED="";
COLORED="";
COUNTER=0;
# ag -i filefoo /bar/
# ag [FILE-TYPE] [OPTIONS] PATTERN [PATH]
# ack [OPTION]... PATTERN [FILES OR DIRECTORIES]
# sift [OPTIONS] PATTERN [FILE|PATH|tcp://HOST:PORT]...
# sift [OPTIONS] [-e PATTERN | -f FILE] [FILE|PATH|tcp://HOST:PORT]...
# sift [OPTIONS] --targets [FILE|PATH]...
# grep [-abcdDEFGHhIiJLlmnOopqRSsUVvwxZ] [-A num] [-B num] [-C[num]] [-e pattern] [-f file]
# [--binary-files=value] [--color[=when]] [--colour[=when]] [--context[=num]]
# [--label] [--line-buffered] [--null] [pattern] [file ...]
# In Bash you can also use test -t to check for a terminal:
if [ -t 0 ]; then
# Terminal input (keyboard) - interactive
PIPED=""
else
# File or pipe input - non-interactive
PIPED="YES"
# REF: http://stackoverflow.com/questions/2746553/bash-script-read-values-from-stdin-pipe
# read PIPED #TOOD Works on mac osx, pending to test in linux and windows
PIPED=''
# REF: https://stackoverflow.com/questions/7314044/use-bash-to-read-line-by-line-and-keep-space
# REF: http://www.unix.com/shell-programming-and-scripting/58611-resetting-ifs-variable.html
OLDIFS=$IFS # resetting IFS variable
IFS='' # to read line by line and keep space
IFS= read -r ONEPIPE # to avoid interpretation of backslashes.
PIPED="${ONEPIPE}"
[[ -n "$PIPED" ]] && COUNTER=$((COUNTER+1))
while read ONEPIPE
do
PIPED="${PIPED}
${ONEPIPE}"
COUNTER=$((COUNTER+1))
done
if [ $COUNTER -eq 0 ]; then
IFS=$OLDIFS
exit
fi
# action="${ONEPIPE/ /⃝}" # replace value inside string substitution expresion bash
# TEST: echo "COUNTER: ${COUNTER}";echo "PIDED: ${PIPED}"; exit 0;
IFS=$OLDIFS
fi
# Piped Input
# if [[ -n "$PIPED" ]]
# then
# echo "this is pipe..die "
# exit 0;
#fi
# NOT Piped Input
#if [ -z "$PIPED" ]
# then
# echo "this NOT pipe..die "
# exit 0;
#fi
#echo "PIPED:$PIPED";
#exit;
# ALTERNATIVE:
#if readlink /proc/$$/fd/0 | grep -q "^pipe:"; then
# Pipe input (echo abc | myscript)
# PIPED="YES"
#elif file $( readlink /proc/$$/fd/0 ) | grep -q "character special"; then
# Terminal input (keyboard)
# PIPED=""
#else
# File input (myscript < file.txt)
# PIPED=""
#fi
# CURRENT SCRIPT EXECUTING
THISSCRIPTNAME=`basename "$0"`
# bash shell script check input argument
FAIL=0;
echo " ";
if [ -z "$1" ] && [ -z "$PIPED" ] ; then
{
echo "Missing 1st argument "
FAIL=1;
}
fi
VERBOSE=0;
DEFAULTSEEKER="pt"
FILENAMESONLY=0
SEEKING="$1";
if [ -z "$2" ]
then
echo "Missing 2nd argument "
FAIL=1;
fi
if [ $FAIL -eq 1 ]
then
echo " "
echo "Sample Usage: - expects two arguments "
echo " "
echo " ${THISSCRIPTNAME} seek_this replaced_it_with"
echo " ";
exit
fi
if [[ "$1" == "-h" ]] && [[ -z "$PIPED" ]] ; then
{
echo "${THISSCRIPTNAME} - Means replace in German"
echo " "
echo "Can be used: - with three arguments "
echo " "
echo " ${THISSCRIPTNAME} seek_this replaced_it_with [ack, sift, grep, ag]"
echo " ";
echo " assuming order or use ag -> ack -> grep -> local ./ack"
echo " ";
exit 0;
}
fi
VERBOSE=0;
DEFAULTSEEKER="pt"
if [ ! -z "$3" ] ; then # Third argument not zempty
# Second Argument provided is -v and is not PIPED or PIDEd is empty
if [[ "$3" == "-v" ]] && [ -z "$PIPED" ] ; then
{
echo "Verbose on";
VERBOSE=1;
}
fi
if [[ "$3" == "sift" || "$3" == "pt" || "$3" == "ack" || "$3" == "grep" || "$3" == "ag" || "$3" == "./ag" || "$3" == "./ack" || "$3" == "./sift" ]] ; then
{
DEFAULTSEEKER="$3"
}
fi
if [[ "$3" == "--nocolor" ]] ; then
{
COLORED="";
}
fi
if [[ "$3" == "-f" ]] && [ -z "$PIPED" ] ; then
{
if [ $VERBOSE -eq 1 ] ; then
{
echo "Files on";
}
fi
FILENAMESONLY=1;
}
fi
fi
if [ ! -z "$4" ] ; then # Third argument not zempty
# Second Argument provided is -v and is not PIPED or PIDEd is empty
if [[ "$4" == "-v" ]] && [ -z "$PIPED" ] ; then
{
echo "Verbose on";
VERBOSE=1;
}
fi
if [[ "$4" == "sift" || "$4" == "pt" || "$4" == "ack" || "$4" == "grep" || "$4" == "ag" || "$4" == "./ag" || "$4" == "./ack" || "$4" == "./sift" ]] ; then
{
DEFAULTSEEKER="$4"
}
fi
if [[ "$4" == "--nocolor" ]] ; then
{
COLORED=""; # "" is no "YES" is yes
}
fi
if [[ "$4" == "-f" ]] && [ -z "$PIPED" ] ; then
{
if [ $VERBOSE -eq 1 ] ; then
{
echo "Files on";
}
fi
FILENAMESONLY=1;
}
fi
fi
# TEST COLORED AND PIPED
# echo "$SEEKING"
# # NOT Colored
# if [[ -n "$COLORED" ]] ; then
# {
# echo "NOT COLORED"
# }
# else
# {
# echo "[38;5;28mCOL[38;5;9mORED[0m"
# }
# fi
# # NOT Piped
# if [ -z "$PIPED" ] ; then
# {
# echo "NOT PIPED"
# }
# else
# {
# echo "[38;5;28mPI[38;5;9mPED[0m"
# if [[ "$SEEKING" == *""* ]] ; then
# {
# echo "$SEEKING"
# echo "BUT PASSED CHARS HAVE COLOR"
# }
# else
# {
# echo "$SEEKING"
# echo "NO COLOR FROM BEHIND"
# }
# fi
# }
# fi
# exit
# Override colored off when piped with not color
# Piped
if [ -n "$PIPED" ] ; then
{
WHAT=$(echo "$PIPED" | grep -E "\[")
#echo "$WHAT";
if [ -z "$WHAT" ] ; then
{
# Colored
COLORED="YES"; # "" is no "YES" is yes
#echo "z"
}
else
{
# NOT Colored
COLORED=""; # "" is no "YES" is yes
#echo "!z"
}
fi
#}
#| sed 's@\@ESC@'
#exit
}
fi
# NOT Colored
# if [ -n "$COLORED" ] ; then
# {
# echo "NOT COLORED"
# }
# else
# {
# echo "[38;5;28mCOL[38;5;9mORED[0m"
# }
# fi
# # exit
#### #### # #### ##### ####
#### #### # #### ##### ####
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # ####
# # # # # # # # ####
# # # # # # ##### #
# # # # # # ##### #
# # # # # # # # # # #
# # # # # # # # # # #
#### #### ###### #### # # ####
#### #### ###### #### # # ####
#
# COLORS - Ouput - Start
#
[[ -z "${RED}" ]] && RED="\\033[38;5;1m"
red=`tput setaf 1`
[[ -z "${CYAN}" ]] && CYAN="\\033[38;5;123m"
cyan=`tput setaf 6`
[[ -z "${GREEN}" ]] && GREEN="\\033[38;5;22m"
green=`tput setaf 2`
[[ -z "${RESET}" ]] && RESET="\\033[0m"
[[ -z "${RESET_PROMPT}" ]] && RESET_PROMPT="[0m"
reset=`tput sgr0`
[[ -z "${BLACK}" ]] && BLACK="\\033[38;5;16m"
black=$(tput setaf 0)
[[ -z "${YELLOW226}" ]] && YELLOW226="\\033[38;5;226m"
[[ -z "${YELLOW}" ]] && YELLOW="\\033[01;33m"
yellow=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
[[ -z "${PURPLE}" ]] && PURPLE="\\033[01;35m"
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)
#
# COLORS - Output - end
#
SEEKER="pt";
IGNORE_DIR="ignore";
FILESCOMMAND="";
PRINTCOMMAND="-l -0";
FOUND=0
msg_red () {
printf "\n${bdr}%s ${off}%s\n" "${red} $@"
}
msg_green () {
printf "\n${bdg}%s ${off}%s\n" "${green} $@"
}
msg_echo () {
echo -e "${*}"
}
trap '{ msg_red " KEYBOARD INTERRUPT."; exit 130; }' INT
#
# C H E C K H O W C O M P U T E R H A N D L E S E R R O R S --Start
#
test_positives(){
KIND=""
if ls werwerwerwerwerwerwerwerwerwerwerr >/dev/null 2>&1; then
if (( $? == 0 )) ; then # Intel processor
KIND="INTEL"
fi
if [ $? == 0 ]; then # Mac Intel processor
KIND="${KIND}MAC"
fi
else
if (( $? == 0 )) ; then # Intel processor
KIND="INTEL"
fi
if [ $? == 0 ]; then # Mac Intel processor
KIND="${KIND}MAC"
fi
fi
echo "${KIND}"
}
PROCESSOR_ERROR=$(test_positives)
# DEBUG MACPOSITIVE MACPOSITIVE echo "${PROCESSOR_ERROR}"
# DEBUG MACPOSITIVE exit 1
#
# C H E C K H O W C O M P U T E R H A N D L E S E R R O R S --End
#
#
# C H E C K R E P L A C E F U N C T I O N S I N S T A L L E D --Start
#
check_replacer () {
local -i _err
local REPLACER="$1"
if command -v "${REPLACER}" >/dev/null 2>&1; then
# It looks installed
# .. is it working properly
# msg_green " ${1} INSTALLED."
#stdout UND stderr -capture REF: https://www.thomas-krenn.com/de/wiki/Bash_stdout_und_stderr_umleiten
${REPLACER} --version &> /tmp/ersetze_test_${REPLACER}.txt
_err=$?
local PROPERLYWORKING=$(cat /tmp/ersetze_test_${REPLACER}.txt)
if [[ "$PROPERLYWORKING" == *"dyld:"* ]]; then { echo "error"; return;} fi
if [[ "$PROPERLYWORKING" == *"GNU"* ]]; then { echo "GNU"; return;} else { echo "MAC";return;} fi
if [ ${_err} -ne 0 ] ; then { echo "error"; return;} fi
echo "checked";
return;
else
# msg_red "${green} ${red} CANNOT REPLACE ...${1} IS MISSING ";
# msg_red " NEED TO INSTALL ${1}. Linux: sudo apt-get install ${1} Mac: brew install ${1} "
echo "install";
return;
fi
}
msg_install () {
msg_red "${green} ${red} CANNOT REPLACE ...${1} IS MISSING ";
msg_red " NEED TO INSTALL ${1}. Linux: sudo apt-get install ${1} Mac: brew install ${1} "
}
# REPLACER="sed";
# Try vim's ex ..broke here grrr TODO research how to implement doing erestese_indatei /images/projects/ images/projects index.html did not work at all
REPLACER="sed"; # changed to sed
VALIDREPLACER=$(check_replacer "${REPLACER}")
if [[ $VALIDREPLACER == "error" ]] ; then
msg_red "Error with replacer ${REPLACER}"
msg_red " - Error:"
cat /tmp/ersetze_test_${REPLACER}.txt
rm /tmp/ersetze_test_${REPLACER}.txt
fi
if [[ $VALIDREPLACER == "install" ]] ; then
msg_install "${REPLACER}"
fi
rm /tmp/ersetze_test_${REPLACER}.txt
# TODO - Remove Repetition HERE
# ? empty still
if [[ $VALIDREPLACER == "install" || $VALIDREPLACER == "error" ]] ; then
REPLACER="sed";
VALIDREPLACER=$(check_replacer "${REPLACER}")
if [[ $VALIDREPLACER == "error" ]] ; then
msg_red "Error with replacer ${REPLACER}"
msg_red " - Error:"
cat /tmp/ersetze_test_${REPLACER}.txt
rm /tmp/ersetze_test_${REPLACER}.txt
exit 1;
fi
if [[ $VALIDREPLACER == "install" ]] ; then
msg_install "${REPLACER}"
rm /tmp/ersetze_test_${REPLACER}.txt
exit 1;
fi
fi
REPLACERGNU="NO"
if [[ $VALIDREPLACER == "GNU" ]] ; then
{
REPLACERGNU="YES"
}
fi
# one more check for gsed from brew in macs
if [[ $REPLACERGNU == "NO" ]] ; then
{
if command -v gsed >/dev/null 2>&1 ; then
{
REPLACERGNU="YES"
REPLACER="gsed"
}
fi
}
fi
XARGSCOMMAD=""
# Test
# echo "REPLACERGNU: $REPLACERGNU"
# echo "VALIDREPLACER: $VALIDREPLACER"
# exit
# C H E C K R E P L A C E F U N C T I O N S I N S T A L L E D -- RESULTS
# Results as
# $REPLACERGNU NO OR YES
# $REPLACERGNU ex or sed
# halts execution if not found
#
# C H E C K R E P L A C E F U N C T I O N S I N S T A L L E D -- End
#
# S E E K I N G I N S T A L L C H E C K - Start
#
if command -v pt >/dev/null 2>&1; then
{
# pt --help
# Usage:
# pt [OPTIONS] PATTERN [PATH]
#
# Application Options:
# --version Show version
#
# Output Options:
# --color Print color codes in results (default: true)
# --nocolor Don't print color codes in results (default: false)
# --color-line-number= Color codes for line numbers (default: 1;33)
# --color-path= Color codes for path names (default: 1;32)
# --color-match= Color codes for result matches (default: 30;43)
# --group Print file name at header (default: true)
# --nogroup Don't print file name at header (default: false)
# -0, --null Separate filenames with null (for 'xargs -0') (default: false)
# --column Print column (default: false)
# --numbers Print Line number. (default: true)
# -N, --nonumbers Omit Line number. (default: false)
# -A, --after= Print lines after match
# -B, --before= Print lines before match
# -C, --context= Print lines before and after match
# -l, --files-with-matches Only print filenames that contain matches
# -c, --count Only print the number of matching lines for each input file.
# -o, --output-encode= Specify output encoding (none, jis, sjis, euc)
#
# Search Options:
# -e Parse PATTERN as a regular expression (default: false). Accepted syntax is the same
# as https://github.com/google/re2/wiki/Syntax except from \C
# -i, --ignore-case Match case insensitively
# -S, --smart-case Match case insensitively unless PATTERN contains uppercase characters
# -w, --word-regexp Only match whole words
# --ignore= Ignore files/directories matching pattern
# --vcs-ignore= VCS ignore files (default: .gitignore)
# --global-gitignore Use git's global gitignore file for ignore patterns
# --home-ptignore Use $Home/.ptignore file for ignore patterns
# -U, --skip-vcs-ignores Don't use VCS ignore file for ignore patterns
# -g= Print filenames matching PATTERN
# -G, --file-search-regexp= PATTERN Limit search to filenames matching PATTERN
# --depth= Search up to NUM directories deep (default: 25)
# -f, --follow Follow symlinks
# --hidden Search hidden files and directories
#
# Help Options:
# -h, --help Show this help message
if [ $VERBOSE -eq 1 ] ; then
{
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
echo " pt INSTALLED."
}
else
{
msg_echo " pt INSTALLED."
}
fi
}
fi
# pt is case sensitive by default
# -0, --null Separate filenames with null (for 'xargs -0') (default: false)
# --nocolor Don't print color codes in results (default: false)
#
# WHEN not PIPED then show line numbers
# -N, --nonumbers Omit Line number. (default: false)
if [ -z "$PIPED" ] ; then
{
SEEKER=" pt --hidden --numbers "
}
fi
# PIPED then hide line numbers
if [[ -n "$PIPED" ]] ; then
{
SEEKER=" pt --hidden --nonumbers "
}
fi
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
SEEKER="${SEEKER} --nocolor ";
}
fi
# -l, --files-with-matches Only print filenames that contain matches
IGNORE_DIR="ignore";
IGNORE_FILE="ignore";
PRINTCOMMAND="-l --null";
XARGSCOMMAD="-0";
}
else # // sift not installed
{
if command -v sift >/dev/null 2>&1; then
{
if [ $VERBOSE -eq 1 ] ; then
{
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
echo " sift INSTALLED."
}
else
{
msg_echo " sift INSTALLED."
}
fi
}
fi
# -i, --ignore-case case insensitive (default: off) Case sentive by default
# -n, --line-number show line numbers (default: off)
# -Q, --literal treat pattern as literal, quote meta characters
# --output-sep= output separator (default: "\n")
# --no-color disable colored output
# Error: sift Error: files skipped due to very long lines
# Fix:
# REF: https://github.com/svent/sift/issues/16
# As of version 0.4.0 sift supports two new options:
#
# --err-skip-line-length to skip these errors
# --blocksize to specify a larger blocksize, allowing to search files with very long lines. Example: --blocksize 10M
SEEKER="sift -nQ --blocksize 50M";
# NOT PIPED then show line numbers
if [ -z "$PIPED" ] ; then
{
SEEKER="sift --err-skip-line-length -nQ ";
}
fi
# PIPED then hide line numbers
if [[ -n "$PIPED" ]] ; then
{
SEEKER="sift --err-skip-line-length -Q ";
}
fi
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
SEEKER="${SEEKER} --no-color ";
}
fi
IGNORE_DIR="exclude-dirs";
IGNORE_FILE="exclude-files";
PRINTCOMMAND="-l --output-sep=\"\\x00\"";
}
else # // pt not installed
{
# msg_red " NEED TO INSTALL pt."
# msg_red " ENTER SUDO PASSWORD AND PRESS ENTER."
# REF: https://github.com/monochromegane/the_platinum_searcher
# sudo brew install the_platinum_searcher
if command -v ag >/dev/null 2>&1; then
{
if [ $VERBOSE -eq 1 ] ; then
{
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
echo " ag INSTALLED."
}
else
{
msg_echo " ag INSTALLED."
}
fi
}
fi
# -s is for case sensitive
#-Q --literal Don't parse PATTERN as a regular expression
#-0 --null --print0 Separate filenames with null (for 'xargs -0')
#--nocolor Disable colors (Enabled by default)
# NOT PIPED then show line numbers
if [[ -z "$PIPED" ]] ; then
{
SEEKER="ag -sQ ";
}
fi
# PIPED then hide line numbers
if [[ -n "$PIPED" ]] ; then
{
SEEKER="ag --no-numbers -sQ ";
}
fi
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
SEEKER="${SEEKER} --nocolor ";
}
fi
IGNORE_DIR="ignore";
IGNORE_FILE="ignore";
PRINTCOMMAND="-l --print0";
}
else # // ag not installed
{
# msg_red " NEED TO INSTALL ag."
# msg_red " ENTER SUDO PASSWORD AND PRESS ENTER."
# sudo apt-get install silversearcher-ag -fy
if command -v ack >/dev/null 2>&1; then
{
if [ $VERBOSE -eq 1 ] ; then
{
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
echo " ack INSTALLED."
}
else
{
msg_echo " ack INSTALLED."
}
fi
}
fi
# ack is case sensitve by default
# --print0 Print null byte as separator between filenames, only works with -f, -g, -l, -L or -c.
# -Q, --literal Quote all metacharacters; PATTERN is litera
# --nocolor Highlight the matching text (default: on unless --nocolor)
#
# Ack DOES NOT SUPPORT HIDING LINE NUMBERS
# NOT PIPED then show line numbers
if [[ -z "$PIPED" ]] ; then
{
SEEKER="ack -Q ";
}
fi
# PIPED then hide line numbers
if [[ -n "$PIPED" ]] ; then
{
SEEKER="ack -Q ";
}
fi
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
SEEKER="${SEEKER} --nocolor ";
}
fi
IGNORE_DIR="ignore-dir";
IGNORE_FILE="ignore-file";
PRINTCOMMAND="-l --print0";
}
else # // ack not installed
{
# msg_red " NEED TO INSTALL ack."
# msg_red " ENTER SUDO PASSWORD AND PRESS ENTER."
# sudo apt-get install ack-grep -fy
ls ./ack | egrep 'ack' &>/dev/null
err_buff=$?
if [[ "${PROCESSOR_ERROR}" == *"MAC"* ]] && [ $err_buff == 0 ]; then
{
if [ $VERBOSE -eq 1 ] ; then
{
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
echo " ./ack INSTALLED."
}
else
{
msg_echo " ./ack INSTALLED."
}
fi
}
fi
# Ack DOES NOT SUPPORT HIDING LINE NUMBERS
# NOT PIPED then show line numbers
if [ -z "$PIPED" ] ; then
{
SEEKER="./ack -Q ";
}
fi
# PIPED then hide line numbers
if [[ -n "$PIPED" ]] ; then
{
SEEKER="./ack -Q ";
}
fi
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
SEEKER="${SEEKER} --nocolor ";
}
fi
IGNORE_DIR="ignore-dir";
IGNORE_FILE="ignore-file";
PRINTCOMMAND="-l --print0";
}
elif [[ "${PROCESSOR_ERROR}" == *"INTEL"* ]] && (( $err_buff == 0 )); then
{
if [ $VERBOSE -eq 1 ] ; then
{
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
echo " ./ack INSTALLED."
}
else
{
msg_echo " ./ack INSTALLED."
}
fi
}
fi
# Ack DOES NOT SUPPORT HIDING LINE NUMBERS
# NOT PIPED then show line numbers
if [ -z "$PIPED" ] ; then
{
SEEKER="./ack -Q ";
}
fi
# PIPED then hide line numbers
if [[ -n "$PIPED" ]] ; then
{
SEEKER="./ack -Q ";
}
fi
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
SEEKER="${SEEKER} --nocolor ";
}
fi
IGNORE_DIR="ignore-dir";
IGNORE_FILE="ignore-file";
PRINTCOMMAND="-l --print0";
}
else # // ./ack not installed
{
# msg_red " NEED TO INSTALL ./ack "
# msg_red " ENTER SUDO PASSWORD AND PRESS ENTER."
# cp ~/bin/ack .
if command -v grep >/dev/null 2>&1; then
{
#-i, --ignore-case
# Perform case insensitive matching. By default, grep is case sensitive.
#
#-E, --extended-regexp
# Interpret pattern as an extended regular expression (i.e. force grep to behave as
# egrep).
#
#-e pattern, --regexp=pattern
# Specify a pattern used during the search of the input: an input line is selected if it
# matches any of the specified patterns. This option is most useful when multiple -e
# options are used to specify multiple patterns, or when a pattern begins with a dash
# (`-').
#-F, --fixed-strings
# Interpret pattern as a set of fixed strings (i.e. force grep to behave as fgrep).
# --color=never disable color
#
# grep (GNU grep) 2.25
# -l, --files-with-matches
# Suppress normal output; instead print the name of each input
# file from which output would normally have been printed. The
# scanning will stop on the first match.
#
# -Z, --null
# Output a zero byte (the ASCII NUL character) instead of the
# character that normally follows a file name. For example, grep
# -lZ outputs a zero byte after each file name instead of the
# usual newline. This option makes the output unambiguous, even
# in the presence of file names containing unusual characters
# like newlines. This option can be used with commands like find
# -print0, perl -0, sort -z, and xargs -0 to process arbitrary
# file names, even those that contain newline characters.
#
if [ $VERBOSE -eq 1 ] ; then
{
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
echo " grep INSTALLED."
}
else
{
msg_echo " grep INSTALLED."
}
fi
}
fi
# WHEN PIPED then show line numbers
if [ -z "$PIPED" ] ; then
{
SEEKER="grep -nrE "
}
fi
# PIPED then hide line numbers
if [[ -n "$PIPED" ]] ; then
{
SEEKER="grep -rE "
}
fi
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
SEEKER="${SEEKER} --color=never ";
}
fi
IGNORE_DIR="exclude-dir";
IGNORE_FILE="exclude";
FILESCOMMAND=".";
# PRINTCOMMAND="-l --print0";
PRINTCOMMAND="-l --null";
}
else # // grep not installed
{
# NOT Colored
if [[ -n "$COLORED" ]] ; then
{
echo "CANNOT SEARCH ....MISSING SEARCHER SIFT, ACK, AG, ./ACK, ./AG, ./SIFT OR GREP ";
exit 1;
}
else
{
echo -e "${RED} CANNOT SEARCH ....MISSING SEARCHER SIFT, ACK, AG, ./ACK, ./AG, ./SIFT OR GREP ";
exit 1;
}
fi
#msg_red " NEED TO INSTALL grep."
}
fi
}
fi
}
fi
}
fi
}
fi
}
fi
# show hidden chars
# echo -e "${CYAN}" | tr -dc '[:print:]' | od -c ;
# echo -e "${RED}" | tr -dc '[:print:]' ;
# exit 1;
#CHECK FOR CHOICE FORCE IT
if command -v "${DEFAULTSEEKER}" >/dev/null 2>&1; then
{
SEEKER="${DEFAULTSEEKER}";
echo "${cyan} using chosen option ... ${SEEKER} "
msg_green " ${SEEKER} INSTALLED."
# make case sentive for local sift
if [ $DEFAULTSEEKER == "sift" ]; then
{
SEEKER="sift -nQ --blocksize 50M";
IGNORE_DIR="exclude-dirs";
IGNORE_FILE="exclude-files"
PRINTCOMMAND="-l --output-sep=\"\\x00\"";
}
fi
# make case sentive for ag
if [[ "$DEFAULTSEEKER" == "ag" ]] ; then
{
SEEKER="ag -sQ "
IGNORE_DIR="ignore";
IGNORE_FILE="ignore";
PRINTCOMMAND="-l --print0";
}
fi
# make case sentive for ag
if [[ "$DEFAULTSEEKER" == "ack" ]] ; then
{
SEEKER="ack -Q "
IGNORE_DIR="ignore-dir";
IGNORE_FILE="ignore-dir";
PRINTCOMMAND="-l --print0";
}
fi
# make case sentive for local ./ack
if [ -e "./ack" ] && [[ "$DEFAULTSEEKER" == "./ack" ]] ; then
{
SEEKER="./ack -Q "
IGNORE_DIR="ignore-dir";
IGNORE_FILE="ignore-file";
PRINTCOMMAND="-l --print0";
}
fi
# use extended expressions
if [[ "$DEFAULTSEEKER" == "grep" ]] ; then
{
SEEKER="grep -rnE "
IGNORE_DIR="exclude-dir";
IGNORE_FILE="exclude";
FILESCOMMAND=".";
# PRINTCOMMAND="-l --print0";
PRINTCOMMAND="-l --null";
}
fi
}
else
{
# echo "${red} your seeker ${DEFAULTSEEKER} was not found!"
echo "${cyan} using ...${SEEKER} "
}
fi
###### # # #### # # # #### # #### # # ####
###### # # #### # # # #### # #### # # ####
# # # # # # # # # # # # ## # #
# # # # # # # # # # # # ## # #
##### ## # # # # #### # # # # # # ####
##### ## # # # # #### # # # # # # ####
# ## # # # # # # # # # # # #