[英]Edit file in unix using SED
我在 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?)?
非常感谢。
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
如果 Perl 对你没问题,你可以这样做:
perl -pe '$f=1 if(/\[Section B\]/);
s/^\$param2=value2$/\$param2=new_value2/ if($f);' < file
解析文件、执行编辑和重组的 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
读者练习:实现参数/值对的删除。
非常简单的解决方案。
ex file1.txt <<"INPUT"
/Section B
/param2
s/value2/new_value2/
:x
INPUT
如果您需要 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.