使用SED在unix中编辑文件

时间:2012-04-06 07:04:00

标签: linux file bash unix 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?)?

非常感谢。

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果Perl对您没问题,您可以这样做:

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

See it

答案 2 :(得分: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

读者练习:实现删除param / value对。

答案 3 :(得分:0)

非常简单的解决方案。

ex file1.txt <<"INPUT"
/Section B
/param2
s/value2/new_value2/
:x
INPUT

答案 4 :(得分:-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>