If there are more than x number of pipes in a csv line, then delete the 2nd instance
2
votes
2
answers
153
views
I have a csv file which is supposed to contain 4 columns of data which include a product number, a title, a url and a price. Each column is separated by a
|
delimiter (this has to be maintained, there are other reasons why I can't switch to an alternative delimiter which I won't go into here). As can be seen in the bottom entry (which is the problem entry in this example) the title contains a pipe, which breaks the pattern, which could potentially causes issues if the data needs to be imported into a database.
5456435121|The making of the blue album|https://www.example1.co.uk|55
1321354567|Wow this example has no imagination|https://www.cherrypickers.co.uk|89
5456456456|King of the Barbarians | Last Man Standing|https://www.babarians.co.uk|79
What I would like to know is, how can I run a command which could effectively analyse the file, and for every line where there are more than 3 pipes( i.e. every line where the title contains a pipe) then delete the 2nd one in that line. This would effectively allow me to remove the pipe(s) in the title if there is one or more present. I don't know how to achieve it.
I would like the file to look like this once processed:
5456435121|The making of the blue album|https://www.example1.co.uk|55
1321354567|Wow this example has no imagination|https://www.cherrypickers.co.uk|89
5456456456|King of the Barbarians Last Man Standing|https://www.babarians.co.uk|79
Asked by neilH
(433 rep)
May 5, 2016, 11:15 AM
Last activity: Apr 23, 2025, 12:07 PM
Last activity: Apr 23, 2025, 12:07 PM