2 # ==============================================================================
3 # portsreinstall library script
4 # - Editing operations on files -
5 # Copyright (C) 2013-2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
9 # ============= Remove lines exactly matching a string from a file =============
12 local item dstpath tmpfile
15 [ -n "$item" ] || return 0
16 [ -e "$dstpath" ] || return 0
17 tmpfile=$dstpath.fileedit_rm_a_line.tmp
18 grep -v -Fx "$item" "$dstpath" 2> /dev/null > $tmpfile || :
19 mv "$tmpfile" "$dstpath"
22 # ============= Add a line to a file if no matching one exists in the file =============
23 fileedit_add_a_line_if_new ()
25 local item dstpath tmpfile dstdir
28 [ -n "$item" ] || return 0
29 [ -n "$dstpath" ] || return 0
30 tmpfile=$dstpath.fileedit_add_a_line_if_new.tmp
33 cp "$dstpath" "$tmpfile"
34 grep -q -Fx "$item" "$dstpath" \
35 || echo "$item" >> $tmpfile
37 dstdir=`dirname "$dstpath"`
39 echo "$item" > $tmpfile
41 mv "$tmpfile" "$dstpath"
44 # ============= Remove lines exactly matching a string from multiple files =============
45 fileedit_rm_a_line_from_files ()
49 [ -n "$item" ] || return 0
52 fileedit_rm_a_line "$item" "$filepath"
56 # ============= Add a line to each of multiple files if no matching one exists in the file =============
57 fileedit_add_a_line_to_files_if_new ()
61 [ -n "$item" ] || return 0
64 fileedit_add_a_line_if_new "$item" "$filepath"
68 # ============= Add multiple lines to a file for what don't match any existing ones in the file =============
69 fileedit_add_lines_if_new ()
71 local dstpath advance tmpfile tmpclip origin dstdir
74 tmpfile=$dstpath.fileedit_add_lines_if_new.tmp
75 tmpclip=${TMPDIR}/fileedit_add_lines_if_new:add
80 grep -q -Fx "$origin" "$dstpath" \
83 if [ "@$advance" = @advance ]
85 cat "$tmpclip" "$dstpath"
87 cat "$dstpath" "$tmpclip"
90 dstdir=`dirname "$dstpath"`
94 mv "$tmpfile" "$dstpath"
97 # ============= Combine (possibly missing) lists into one =============
98 fileedit_combine_lists ()
100 cat "$@" 2> /dev/null | sort -u || :
103 # ============= Manipulate old and new lines from an old and a new versions of a file =============
104 fileedit_manipulate_old_new_lines ()
106 local oldsrc newsrc olddiff newdiff tmpdiff
111 [ -e "$oldsrc" ] || oldsrc=/dev/null
112 [ -e "$newsrc" ] || newsrc=/dev/null
113 grep -vFx -f "$newsrc" "$oldsrc" > $olddiff 2> /dev/null || :
114 grep -vFx -f "$oldsrc" "$newsrc" > $newdiff 2> /dev/null || :
115 [ `cat "$olddiff" "$newdiff" | wc -l` -gt 0 ]
118 # ============= Manipulate old lines from an old and a new versions of a file =============
119 fileedit_manipulate_old_lines ()
124 [ -e "$oldsrc" ] || oldsrc=/dev/null
125 [ -e "$newsrc" ] || newsrc=/dev/null
126 grep -vFx -f "$newsrc" "$oldsrc" 2> /dev/null
129 # ============= Manipulate new lines from an old and a new versions of a file =============
130 fileedit_manipulate_new_lines ()
132 local oldsrc newsrc tmpdiff
135 [ -e "$oldsrc" ] || oldsrc=/dev/null
136 [ -e "$newsrc" ] || newsrc=/dev/null
137 grep -vFx -f "$oldsrc" "$newsrc" 2> /dev/null
140 # ============= Check whether removed lines exists in updating from an old and a new versions of a file =============
141 fileedit_exists_old_lines ()
143 local oldsrc newsrc tmpdiff
146 [ -e "$oldsrc" ] || oldsrc=/dev/null
147 [ -e "$newsrc" ] || newsrc=/dev/null
148 grep -qvFx -f "$newsrc" "$oldsrc" 2> /dev/null