Wednesday, December 29, 2021

[SOLVED] How to use sed to delete a line that has some determined fields?

Issue

I have this .txt file in linux that looks like this:

Car;Weight;Year;Origin   
Ford Focus;85.2:75:US
Audi A4;78.2;80;Europe
Ford Focus;77.6;90;US

(The actual file is longer but is for you to have an idea of what it looks like) The thing is that I want to delete the line with the Ford Focus whose weigh is 85.2 and year 75, I've tried using sed this way:

sed -i `/Ford Focus/85.2/75/d` cars.txt

But it doesn't work, any idea of how could I do it?


Solution

With ; and : as possible field separators and awk:

awk -F '[;:]' '$1=="Ford Focus" && $2=="85.2" && $3=="75"{next} {print}' cars.txt

Output:

Car;Weight;Year;Origin   
Audi A4;78.2;80;Europe
Ford Focus;77.6;90;US


Answered By - Cyrus