File: sort.sh

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (175 lines) | stat: -rwxr-xr-x 4,541 bytes parent folder | download
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
#! /usr/bin/env bash
set -e -u
# !! EDITS TO THIS FILE ARE LOST DURING UPDATES BY xrst.git/bin/dev_tools.sh !!
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
# SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
# SPDX-FileContributor: 2023-25 Bradley M. Bell
# -----------------------------------------------------------------------------
# bin/sort.sh file_name
# Checks all the sections between
#  BEGIN_SORT_THIS_LINE_PLUS_#
#  END_SORT_THIS_LINE_MINUS_#
# in file_name are sorted. If not, it is corrected and an error is returned.
# -----------------------------------------------------------------------------
# fix sort order; see
# unix.stackexchange.com/questions/87745/what-does-lc-all-c-do/87763#87763
export LC_ALL='C'
#
if [ "$#" == 0 ]
then
cat<< EOF
usage: sort.sh file_name
BEGIN_SORT_THIS_LINE_PLUS_nb: defines beginning line for sort.
END_SORT_THIS_LINE_MINUS_ne:  defines ending line for sort.
The tokens nb and ne are both a single non-zero decimal digit.

If the exit status is 0, the last line on standard out is one of the following:
'sort.sh: OK'    The file was already sorted
'sort.sh: Done'  The file was sorted
EOF
   exit 1
fi
file_name="$1"
#
# grep, sed
source bin/grep_and_sed.sh
#
if $grep 'BEGIN_SORT_THIS_LINE_PLUS_[1-9][0-9]' "$file_name"
then
   echo "in BEGIN_SORT_THIS_LINE_PLUS_nb in file $file_name"
   echo 'nb has more that one decial digit.'
   exit 1
fi
if $grep 'END_SORT_THIS_LINE_MINUS_[1-9][0-9]' "$file_name"
then
   echo "in END_SORT_THIS_LINE_PLUS_ne in file $file_name"
   echo 'ne has more that one decial digit.'
   exit 1
fi
# --------------------------------------------------------------------------
#
# is_file_executable
if [ -x $file_name ]
then
   is_file_executable='yes'
else
   is_file_executable='no'
fi
#
# begin_sum
# is th sum for the beginning line number
set +e
begin_sum=`$grep --line-number 'BEGIN_SORT_THIS_LINE_PLUS_[1-9]' $file_name | \
   $sed -e 's|\([0-9]*\):.*BEGIN_SORT_THIS_LINE_PLUS_\([1-9]\).*|\1+\2|'`
set -e
if [ "$begin_sum" == '' ]
then
   echo "sort.sh $file_name"
   echo "Cannot find BEGIN_SORT_THIS_LINE_PLUS_nb in $file_name"
   exit 1
fi
#
# begin_line
begin_count=0
for sum in $begin_sum
do
   # This does the summation
   let begin_line[$begin_count]="$sum"
   let begin_count="$begin_count + 1"
done
#
# end_diff
# ios the difference for the ending line number
set +e
end_diff=`$grep --line-number 'END_SORT_THIS_LINE_MINUS_[1-9]' $file_name | \
   $sed -e 's|\([0-9]*\):.*END_SORT_THIS_LINE_MINUS_\([1-9]\).*|\1-\2|'`
set -e
if [ "$end_diff" == '' ]
then
   echo "sort.sh $file_name"
   echo "Cannot find END_SORT_THIS_LINE_MINUS_nb in $file_name"
   exit 1
fi
#
# end_line
end_count=0
for diff in $end_diff
do
   # This does the difference
   let end_line[$end_count]="$diff"
   let end_count="$end_count + 1"
done
if [ $begin_count != $end_count ]
then
   echo "sort.sh $file_name"
   echo 'number of BEGIN_SORT_THIS_LINE_PLUS_nb is not equal to'
   echo 'number of END_SORT_THIS_LINE_MINUS_ne.'
   exit 1
fi
#
# first_line
first_line='1'
#
# last_line
last_line=`wc -l $file_name | $sed -e 's|^ *\([0-9]*\) .*|\1|'`
#
# count, stop_line_previous, sorted.$$
count=0
stop_line_previous=0
cp $file_name sorted.$$
while [ $count -lt $begin_count ]
do
   #
   # start_line, stop_line, count
   start_line=${begin_line[$count]}
   stop_line=${end_line[$count]}
   let count="$count + 1"
   echo "sort.sh: sorting lines $start_line to $stop_line in $file_name"
   #
   if [ $start_line -gt $stop_line ]
   then
      echo "start_line = $start_line >= stop_line = $stop_line "
      rm sorted.$$
      exit 1
   fi
   #
   # stop_line_previous
   if [ $stop_line_previous -ge $start_line ]
   then
      echo "previous stop_line=$stop_line_previous >= start_line=$start_line"
      rm sorted.$$
      exit 1
   fi
   stop_line_previous="$stop_line"
   #
   # temp.$$
   if [ "$start_line" != "$first_line" ]
   then
      let start_m1="$start_line - 1"
      $sed -n -e "1,${start_m1}p" sorted.$$ >> temp.$$
   fi
   #
   $sed -n -e "${start_line},${stop_line}p" sorted.$$ | sort >> temp.$$
   #
   if [ "$stop_line" != "$last_line" ]
   then
      stop_p1=`expr $stop_line + 1`
      $sed -n -e "${stop_p1},${last_line}p" sorted.$$ >> temp.$$
   fi
   #
   # sorted.$$
   mv temp.$$ sorted.$$
done
if diff sorted.$$ $file_name > /dev/null
then
   rm sorted.$$
   echo 'sort.sh: OK'
else
   mv sorted.$$ $file_name
   if [ "$is_file_executable" == 'yes' ]
   then
      chmod +x $file_name
   fi
   echo 'sort.sh: Done'
fi
exit 0