1 |
From: Amadeusz Żołnowski <aidecoe@g.o> |
2 |
|
3 |
awk doesn't have the -i option like sed and if editing file in place is |
4 |
desired, additional steps are required. eawk uses tmp file to make it |
5 |
look to the caller editing happens in place. |
6 |
|
7 |
New version of gawk (not stabilized yet) does support editing in place |
8 |
but forcing user to install specific awk implementation is not desired. |
9 |
--- |
10 |
eclass/eutils.eclass | 16 +++++++++++ |
11 |
eclass/tests/eutils_eawk.sh | 65 +++++++++++++++++++++++++++++++++++++++++++++ |
12 |
2 files changed, 81 insertions(+) |
13 |
create mode 100755 eclass/tests/eutils_eawk.sh |
14 |
|
15 |
diff --git a/eclass/eutils.eclass b/eclass/eutils.eclass |
16 |
index dbedffe..963a692 100644 |
17 |
--- a/eclass/eutils.eclass |
18 |
+++ b/eclass/eutils.eclass |
19 |
@@ -20,6 +20,22 @@ _EUTILS_ECLASS=1 |
20 |
|
21 |
inherit multilib toolchain-funcs |
22 |
|
23 |
+# @FUNCTION: eawk |
24 |
+# @USAGE: <file> <args> |
25 |
+# @DESCRIPTION: |
26 |
+# Edit file <file> in place with awk. Pass all arguments following <file> to |
27 |
+# awk. |
28 |
+eawk() { |
29 |
+ local f="$1"; shift |
30 |
+ local tmpf="$(emktemp)" |
31 |
+ |
32 |
+ awk "$@" "${f}" >"${tmpf}" || die -n 'awk failed' || return |
33 |
+ # Following commands should always succeed unless something weird is going |
34 |
+ # on. |
35 |
+ cat "${tmpf}" >"${f}" || die 'failed to replace source file' || return |
36 |
+ rm "${tmpf}" || die "failed to remove temporary file" |
37 |
+} |
38 |
+ |
39 |
# @FUNCTION: eqawarn |
40 |
# @USAGE: [message] |
41 |
# @DESCRIPTION: |
42 |
diff --git a/eclass/tests/eutils_eawk.sh b/eclass/tests/eutils_eawk.sh |
43 |
new file mode 100755 |
44 |
index 0000000..b06f377 |
45 |
--- /dev/null |
46 |
+++ b/eclass/tests/eutils_eawk.sh |
47 |
@@ -0,0 +1,65 @@ |
48 |
+#!/bin/bash |
49 |
+# Copyright 1999-2016 Gentoo Foundation |
50 |
+# Distributed under the terms of the GNU General Public License v2 |
51 |
+# $Id$ |
52 |
+ |
53 |
+source tests-common.sh |
54 |
+ |
55 |
+inherit eutils |
56 |
+ |
57 |
+# Mock die so it doesn't break tests. |
58 |
+die() { |
59 |
+ echo "die: $*" 1>&2 |
60 |
+ return 1 |
61 |
+} |
62 |
+ |
63 |
+tbegin "preserves permissions" |
64 |
+ |
65 |
+cd "$(emktemp -d)" |
66 |
+ |
67 |
+cat <<EOF >'test.txt' |
68 |
+testme1 |
69 |
+testme2 |
70 |
+testme3 |
71 |
+EOF |
72 |
+ |
73 |
+cat <<EOF >'test_expected.txt' |
74 |
+testme1 |
75 |
+foo |
76 |
+testme3 |
77 |
+EOF |
78 |
+ |
79 |
+chmod 704 'test.txt' |
80 |
+eumask_push 000 |
81 |
+eawk 'test.txt' '/^testme2$/ {print "foo"; next;} 1' |
82 |
+eumask_pop |
83 |
+ |
84 |
+diff 'test.txt' 'test_expected.txt' |
85 |
+expected=$? |
86 |
+ |
87 |
+[[ $(stat -c '%a' 'test.txt') = 704 ]] |
88 |
+perms=$? |
89 |
+ |
90 |
+[[ ${expected}${perms} = 00 ]] |
91 |
+ |
92 |
+tend $? |
93 |
+ |
94 |
+ |
95 |
+tbegin "doesn't alter file on failure" |
96 |
+ |
97 |
+cd "$(emktemp -d)" |
98 |
+ |
99 |
+cat <<EOF >'test.txt' |
100 |
+testme1 |
101 |
+testme2 |
102 |
+testme3 |
103 |
+EOF |
104 |
+ |
105 |
+cat 'test.txt' >'test_expected.txt' |
106 |
+ |
107 |
+eawk 'test.txt' '/^testme2$/ print "foo"; next;} 1' 2>/dev/null |
108 |
+diff 'test.txt' 'test_expected.txt' |
109 |
+ |
110 |
+tend $? |
111 |
+ |
112 |
+texit |
113 |
-- |
114 |
2.8.2 |