[英]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.