Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

shell - Awk command to add and delete lines from a block

my input file has blocks like

[abc]  
para1=123  
para2=456  
para3=111  
para4=666

[pqr]  
para1=333    
para2=765    
para3=1345    

[xyz]    
para1=888    
para2=236    

I have two requirements:

  1. Add a line to a block, ex: add ???????para4=uiy to block [pqr]
  2. To delete a parameter from a block, ex: delete para2=236 from block [xyz]

Please help me how can I achieve this with awk command

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add a param

awk -F"
" '$1~/pqr/{$(NF+1)="para4=uiy"}1' OFS="
" RS= file

If it always adds the last one and you dont know the number you could use

awk -F"
" '$1~/pqr/{$(NF+1)="para"NF"=uiy"}1' OFS="
" RS=

Remove a param

awk -F"
" '$1~/xyz/{gsub(/
para2=236/,"")}1' OFS="
" RS= file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...