-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-shell-segments.awk
executable file
·111 lines (101 loc) · 2.94 KB
/
get-shell-segments.awk
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
#!/usr/bin/awk -f
###############################################################################
# PURPOSE: Print out commands found inside Markdown 'console' sessions.
#
# AUTHOR: DCD
# DATE: September 2023
#
# RETURNS: 0 - OK
# 1 - FAIL
#
# EXAMPLE: ./get-shell-segments.awk SOME_FILE.md
#
# NOTE:
###############################################################################
BEGIN {
FALSE = 0;
TRUE = 1;
count = 0;
insegment = 0;
commmands = 0;
sudos = 0;
}
{
## if we are inside a console then set 'insegment' to true
if( $0 ~ /^```console$/ ) {
if( ! insegment ) {
count = count +1;
insegment = 1;
}
}
else {
if( $0 ~ /^```$/ ) {
## then we are no longer in a console segment; set the var to false
insegment = 0;
}
else {
if( insegment ) {
keep = TRUE;
## look for the '$ ' or the '# ' prompt at the beginning of a line
if( $0 ~ /^[\$#][ ]/ ) {
## in case the first command is 'sudo' then count and remove it:
if( $0 ~ /^[\$#] sudo / ) {
## if we are doing a 'sudo -' we will count it, but drop the cmd
sudos = sudos + 1;
if( $0 ~ /^[\$#] sudo -/ ) {
keep = FALSE;
}
else {
sub( /sudo /, "", $0 );
}
commands = commands + 1;
}
## Here we watch for environment variables in front of commands
## For example: TZ='America/Vancouver' date
if( $2 ~ /=/ ) {
## too few words (probably setting a var) so throw this command out
if( NF <= 2 ) {
keep = FALSE;
}
else {
## we pick the 3rd word
if( length($3) > 0 && keep ) {
printf( "%s\n", $3 );
commands = commands + 1;
}
}
}
else {
if( keep ) {
if( $2 == "." || length($2) <= 0 ) { ## empty command or '.'
keep = FALSE;
}
else if( $2 ~ /<TAB>/ ) { ## ignore 'TAB' char examples
keep = FALSE;
}
else {
printf( "%s\n", $2 );
commands = commands + 1;
}
## handle commands on the other side of a pipe as well
if( $0 ~ /\|/ && keep ) {
if( split($0, arr, /\|/) > 0 ) {
nf = split(arr[2], arr2, / /); ## split second half by spaces
if( nf > 0 ) {
printf( "%s\n", arr2[2] );
commands = commands + 1;
}
}
}
}
}
}
}
}
}
}
#END {
# printf( "Number of shell segments is %i\n", count );
# printf( "Number of commands is %i\n", commands );
# printf( "Number of sudo commands is %i\n", sudos );
#}