Your sed
command appears to be replacing all non-overlapping occurrences of the string ||
with |00000000000|
. It is unclear how this does not solve your issue, at least for the data supplied:
% sed 's/||/|00000000000|/g' file.csv
0000000001|00346743139|201901|07
0000000002|00000000000|201901|00
Note that your expected output seems to truncate 00346743139
into 003467431
(the last two integers are removed). It is unclear whether this is intentional.
If there is an issue in using the non-standard -i
option with the sed
implementation on your Unix, then please see the post How can I achieve portability with sed -i (in-place editing)?
For example, on macOS, your command would give you
$ sed -i "s/||/|00000000000|/g" filename
sed: 1: "filename": invalid command code f
due to the way the -i
option is used differently on that system. Use -i ''
instead on macOS (and read man sed
on your system about this option).
For more robust CSV parsing, you may want to use an actual CSV parser.
Using the csvkit tools to do the CSV parsing, and jq
to do the actual processing:
% csvjson -I -H file.csv | jq -r '.[] | map(select(. == null) |= "00000000000") | @csv' | csvformat -D '|'
0000000001|00346743139|201901|07
0000000002|00000000000|201901|00
This first converts your CSV file to JSON using csvjson
. The options we use here turns off type inference (so that the numbers are interpreted as strings), and tells the utility that there is no header line in the CSV data. The csvjson
tool will automatically detect that |
is the correct delimiter used in the data, but you may also explicitly tell it to use |
as the delimiter using -d '|'
.
The jq
code then replaces all empty values with the string 00000000000
and formats the processed data into CSV again.
Since you want to have pipe-delimited output, csvformat
is used to change the delimiters outputted by @csv
in jq
into |
-characters.
Redirect the output to a new filename, and then optionally replace the original data with that new file.
Both csvkit
and jq
are available for macOS via the Homebrew package manager.
sed
command? Error messages? Unexpected output? We weren't with you so you need to describe what happened – Chris Davies Jan 12 '21 at 07:1300346743139
should became003467431
? Your sed command is good, the modification is done in place check your file – Ôrel Jan 12 '21 at 07:49