回答編集履歴

2

改行削除

2017/10/11 11:39

投稿

退会済みユーザー
test CHANGED
@@ -6,9 +6,7 @@
6
6
 
7
7
  ```bash
8
8
 
9
- sed -n '/^<aaa>$/,/^</xxx>$/p' hoge.txt | sed -e 's/^<aaa>$/<bbb>/g' -e 's/^</xxx>$/</zzz>/g'
9
+ sed -n '/^<aaa>$/,/^</xxx>$/p' hoge.txt | sed -e 's/^<aaa>$/<bbb>/g' -e 's/^</xxx>$/</zzz>/g' > hogehoge.txt
10
-
11
- > hogehoge.txt
12
10
 
13
11
  ```
14
12
 

1

perl追記

2017/10/11 11:39

投稿

退会済みユーザー
test CHANGED
@@ -1,7 +1,73 @@
1
1
  perlでないとだめでしょうか。
2
+
3
+
4
+
5
+ sedならこう。
6
+
7
+ ```bash
8
+
9
+ sed -n '/^<aaa>$/,/^</xxx>$/p' hoge.txt | sed -e 's/^<aaa>$/<bbb>/g' -e 's/^</xxx>$/</zzz>/g'
10
+
11
+ > hogehoge.txt
2
12
 
3
13
  ```
4
14
 
15
+ ---
16
+
17
+
18
+
19
+ perlならこうでしょうか。
20
+
21
+ ```perl
22
+
23
+ #!/usr/bin/perl
24
+
25
+
26
+
27
+ use utf8;
28
+
29
+ use strict;
30
+
31
+
32
+
33
+ open(IN,"<","hoge.txt");
34
+
35
+
36
+
37
+ open(OUT,">>","hogehoge.txt")or die"error:$!";
38
+
39
+
40
+
41
+ my $text = '';
42
+
43
+ while (my $line = <IN>) {
44
+
45
+ $text .= $line;
46
+
47
+ }
48
+
49
+
50
+
51
+ while($text =~ /<aaa>.*?</xxx>/s){
52
+
53
+ my $tmp = $&;
54
+
55
+ my $match = $&;
56
+
57
+ $match =~ s/^<aaa>$/<bbb>/m;
58
+
5
- sed -n '/^<aaa>$/,/^</xxx>$/p' hoge.txt > hogehoge.txt
59
+ $match =~ s/^</xxx>$/</zzz>\n/m;
60
+
61
+ print OUT $match;
62
+
63
+ $text =~ s/$tmp//s;
64
+
65
+ }
66
+
67
+
68
+
69
+ close(IN);
70
+
71
+ close(OUT);
6
72
 
7
73
  ```