I need a command that replaces a specific pattern in each line of a file as many times as necessary until the pattern is no longer found.
For example, in a CSV file, the fields are separated by a semicolon ;
.
Null fields do not have a character, as in the following file that represents a contact list with 3 records:
Nome;Sobrenome;Telefone1;Telefone2;Email
Joao;Silva;9999-8888;9292-9292;[email protected]
Maria;Souza;8899-0011;;[email protected]
Carlos;Oliveira;;;
The first line is the header of the file. The Maria Souza
contact has the Telefone2
null and the Carlos Oliveira
contact has null the Telefone1
, Telefone2
, and Email
fields.
I want to add \N
where the field is null.
On Linux, use the command:
$ sed -e 's/;;/;\N;/g' -e 's/;$/;\N/' arquivo.csv > novo-arquivo.csv
The result is satisfactory for register Maria Souza
, but not for Carlos Oliveira
, because finding the first ;;
pattern and performing substitution ( Carlos;Oliveira;\N;;
) it does not consider substitute text in the continuation of the search and passes to the next pattern, which is ;$
, with the result being as follows:
Carlos;Oliveira;\N;;\N
Remaining a null field yet.
I would like a solution both Unix and Windows.