-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline-number
executable file
·95 lines (84 loc) · 3.96 KB
/
line-number
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
#! /bin/sh
# Usage
# line-number file_name
# $XDG_CONFIG_HOME/tmux/line-number $XDG_CONFIG_HOME/tmux/prefix.conf
# Match strings '# [0000] or "# [0000] or '# [0000] or "# [0000]
file_name="${1:?"File name must be given"}"
# Prototype
# grep -Hn "[[0-9]]" "$file_name" | awk -F \[ '{print $2}' | awk -F ] '{print $1}'
[ "${file_name:$((${#file_name} - 1))}" != '/' ] ||
file_name="${file_name%${file_name##*[!/]}}"
type print > /dev/null 2>&1 ||
alias print='printf "[%6.6d] %-$((${LENGTH_HEADER:-59} - 10))s '"'"'%s'"'"'\n" "$LINENO"'
[ -f "$file_name" ] ||
{
print 'File does not exist' "$file_name"
exit 1
}
is_integer() {
local char="${1-}"
case "${char#[+-]}" in
(*[!0123456789]*) return 1 ;;
('') return 1 ;;
(*) return 0 ;;
esac
}
backup() {
local file_name="$1"
local tmp_dir="/tmp/$(id -u)"
[ -d "$tmp_dir" ] || \mkdir -p "$tmp_dir"
\cp -f "$file_name" "$tmp_dir/${file_name##*/}"
}
lines="$(wc -l "$file_name" | cut -f 1 -d ' ')"
lines="$((4 > lines? 4 : lines))"
! : ${WIDTH_LINES:=${#lines}} || export WIDTH_LINES
backuped=1
while IFS="$newline" read -r line; do
line_num="$(printf '%s' "$line" | awk -F : '{print $2}')"
line_num_fomated="$(printf "%${WIDTH_LINES}.${WIDTH_LINES}d" "$line_num")"
line_num_fomated_bracketed="[$line_num_fomated]"
# print '$line_num_fomated_bracketed' "$line_num_fomated_bracketed"
# print '$line_num_fomated' "$line_num_fomated"
line_content="$(printf '%s' "$line" | cut -f3- -d':')"
# print '$line_content' "$line_content"
# https://www.unix.com/shell-programming-and-scripting/172036-replace-specific-field-specific-line-sed-awk.html
# line_num_record_bracketed="$(printf '%s' "$line" | sed "/display/{s/.*\(\[[0-9]*]\).*/\1/g}" )"
# correct=0
rest="${line_content}"
# while [ ! -z "$rest" ]; do
# # Only the last bracketed number
# line_num_record_bracketed="$(printf '%s' "$rest" | sed "s/.*\(\[[0-9]\+\]\).*/\1/g" )"
# rest="${rest%${line_num_record_bracketed}*}"
# print "# $line_num_fomated_bracketed \$line_num_record_bracketed" "$line_num_record_bracketed"
# done
while [ ! -z "$rest" ]; do
# line_num_record="$(printf '%s' "$line_content" | awk -F \[ '{print $2}' | awk -F ] '{print $1}')"
# Will lost following candidates
# line_num_record_commented="#$(printf '%s' "$rest" | awk -F \# '{print $2}' | awk -F ] '{print $1}')]"
line_num_record_commented="#$(printf '%s' "$rest" | cut -f2- -d'#')"
printf '%s\n' "$line_num_record_commented" | grep -q "#[[:blank:]]\+\[[0-9]\+\]" || break
print "# $line_num_fomated_bracketed \$line_num_record_commente" "$line_num_record_commented"
line_num_record="$(printf '%s' "$line_num_record_commented" | awk -F \[ '{print $2}' | awk -F ] '{print $1}')"
[ ! -z "$line_num_record" ] || break
is_integer "$line_num_record" || break
rest="${line_num_record_commented#*\["${line_num_record}"\]}"
# print "# $line_num_fomated_bracketed [\$line_num_record]" "[$line_num_record]"
[ "$line_num_fomated" = "$line_num_record" ] || # correct=1
# [ "$correct" -eq 0 ] ||
{
[ "$backuped" -eq 0 ] || { backup "$file_name"; backuped=0; }
# line_content="$(printf '%s' "$line" | awk -F : '{print $3}')" # will lost $4 and later
# Works
# line_content="$(printf '%s' "$line" | awk -F : '{ for (i = 3; i <= NF; i++) if(i == NF) { printf $i } else { printf $i " : " }; print "" }')"
# Works
# line_content="$(printf '%s' "$line" | cut -f3- -d':')"
print "Line $line_num original" "${newline}$line_content"
line_new="$(printf '%s' "$line_content" | sed -e "s/\[$line_num_record\]/\[$line_num_fomated\]/g")"
print "Line $line_num should be" "${newline}$line_new"
# sed -i.backup "${line_num}s/$line_num_record/$line_num_fomated/" "$file_name"
sed -i -e "${line_num}s/$line_num_record/$line_num_fomated/" "$file_name"
print "Line $line_num result" "${newline}$(sed "${line_num}!d" "$file_name")"
}
done
# done < <(grep -Hn "[\"\\']#[[:blank:]]\+\[[0-9]\+\]" "$file_name")
done < <(grep -Hn "#[[:blank:]]\+\[[0-9]\+\]" "$file_name")