2

我在 UNIX 中有 file1.txt 如下

[Section A]
$param1=value1
$param2=value2

[Section B]
$param1=value1
$param2=value2
$param3=value3

我想以编程方式将 B 部分中的 value2 编辑为new_value2

[Section A]
$param1=value1
$param2=value2

[Section B]
$param1=value1
$param2=new_value2
$param3=value3

知道执行此操作的 unix 命令应该是什么(使用 sed 吗?)?

非常感谢。

4

5 回答 5

3
sed -ie '/^\[Section B\]$/,/^$/s/^\$param2=value2$/$param2=new_value/' foo.txt

编辑:上面的例子对旧值和空格字符非常严格。我添加了另一个可能更合适的示例。sed 脚本由一个命令组成,并以以下地址范围为前缀:

/^\[Section B\]/,/^\[.*\]/

地址范围由两个用逗号分隔的正则表达式组成,并将以下命令限制为从第一个地址匹配的行开始,一直持续到第二个地址匹配(包括)。

s/^\(\$param2[ \t]*=[ \t]*\).*$/\1new_value/

替换命令用范围进行实际替换。一切都在一起:

sed -ie '/^\[Section B\]/,/^\[.*\]/s/^\(\$param2[ \t]*=[ \t]*\).*$/\1new_value/' foo.txt
于 2012-04-06T07:11:40.933 回答
0

非常简单的解决方案。

ex file1.txt <<"INPUT"
/Section B
/param2
s/value2/new_value2/
:x
INPUT
于 2012-04-08T10:55:40.577 回答
0

解析文件、执行编辑和重构的 TXR 程序:

@;
@; grab four comand line arguments
@;
@(next :args)
@(cases)
@file
@new_section
@new_param
@new_value
@(or)
@(throw "arguments needed: file section param value")
@(end)
@;
@; hash table mapping sections to assocation lists of values
@;
@(bind sec @(hash :equal-based))
@;
@; parse file, obtaining list of section names and filling in
@; section hash with an associ list of entries.
@;
@(next file)
@(collect)
[Section @secname]
@  (collect)
$@param=@val
@  (until)

@  (end)
@(do (set [sec secname] [mapcar cons param val]))
@(end)
@;
@; now edit
@;
@(do (let ((sec-entries [sec new_section]))
       (if (null sec-entries)
         (push new_section secname))
       (set [sec new_section] (acons-new new_param new_value sec-entries))))
@;
@; now regurgitate file
@;
@(do (each* ((s secname)
             (ent (mapcar (op sec) s)))
       (format t "[Section ~a]\n" s)
       (each ((e ent))
         (format t "$~a=~a\n" (car e) (cdr e)))
       (put-string "\n")))

测试运行:

# edit section B param2 to new_value2

$ txr config.txr config.txt B param2 new_value2
[Section A]
$param1=value1
$param2=value2

[Section B]
$param1=value1
$param2=new_value2
$param3=value3

# add new parameter x with value y to section A

$ txr config.txr config.txt A x y
[Section A]
$x=y
$param1=value1
$param2=value2

[Section B]
$param1=value1
$param2=value2
$param3=value3

# add new section with new parameter

$ txr config.txr config.txt foo bar xyzzy
[Section foo]
$bar=xyzzy

[Section A]
$param1=value1
$param2=value2

[Section B]
$param1=value1
$param2=value2
$param3=value3

读者练习:实现参数/值对的删除。

于 2012-04-06T18:01:01.853 回答
0

如果 Perl 适合你,你可以这样做:

perl -pe '$f=1 if(/\[Section B\]/);
          s/^\$param2=value2$/\$param2=new_value2/ if($f);' < file

看见

于 2012-04-06T07:11:47.977 回答
-1

如果您需要 awk 中的解决方案:

nawk -F= '{if($0~/Section B/){print;getline;print;getline;gsub(/value2/,"value9",$2);print}else print}' file3

测试如下:

pearl.274> cat file3
[section A]
$param1=value1
$param2=value2

[Section B]
$param1=value1
$param2=value2
$param3=value3
pearl.275> nawk -F= '{if($0~/Section B/){print;getline;print;getline;gsub(/value2/,"new_value2",$2);print}else print}' file3
[section A]
$param1=value1
$param2=value2

[Section B]
$param1=value1
$param2 new_value2 
$param3=value3
pearl.276> 
于 2012-04-06T07:19:08.290 回答