lk121
lk121

Reputation: 1325

Replace lines that matches two patterns

Hi i am writing shell script in which i have to replace status O to B by referring two parameters as follows

1. trng-linx |  17.2.18. | change status to O to P in project tasklist | O | 1m
2. trng-lvk1 |  17.2.18. | change P to O in project tasklist | O | 1m

I want to change the status from | O | to | B | for the line (in this example, the first line) which matches the patterns trng-linx and 17.2.18.

I tried using this

sed -i '/^.*\(17\.2\.18\.\).*/s/O/B/' tasklist.txt

but it replaces status of both lines instead of just the first line.

This is my desired output:

1. trng-linx |  17.2.18. | change status to O to P in project tasklist | B | 1m
2. trng-lvk1 |  17.2.18. | change P to O in project tasklist | O | 1m

Please help me to figure it out.

Upvotes: 3

Views: 116

Answers (2)

Endoro
Endoro

Reputation: 37589

GNU sed

sed '/trng-linx |  17.2.18. |/s/O\s|/B\ |/' file

Upvotes: 3

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

Try this:

 awk -F'|' '{OFS="|"; if ($1 ~ /trng-linx/ && $2 ~ /17\.2\.18\./) $4=" B "}1' tasklist.txt

Upvotes: 1

Related Questions