Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

shell - How to use sed to get matched pattern and replace another line text

I want use sed to replace a pattern.

  1. Go through each row
  2. Get each title, left of ":" (Line1) save its content "Line1_content"
  3. Check the "to be replaced part" on the right side of ":" in each row. find matched pattern "Line1" , and then replace it with saved content "Line1_content".

I prefer to use sed to save pattern and swap it to the matched part.

Example input:

Line1: content_A
Line2: content_B 
Line3: Line1
Line4: nonsense & Line2

Example output:

Line1: content_A
Line2: content_B 
Line3: content_A
Line4: nonsense & content_B


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

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...