Remove first comma from csv file in bash

I have a csv file in which some lines begin with a comma, so I want to delete all of them.

Example: In this line:, ,"a","b","c"I want to remove the first comma.

How to do this in bash?

+4
source share
3 answers

You can use this sed:

sed -i '' 's/^[[:blank:]]*,//' file.csv

^[[:blank:]]*, will match a comma at the beginning of the line with optional spaces before the comma.

+3
source

try it;

sed 's/^,//' csvFile > newCSVfile

Example

user@host:/tmp$ echo ',"a","b","c"' | sed 's/^,//'
"a","b","c"
+1
source

Do not use the "g" flag in sed, it will help you remove only the first match ","

echo ',"a","b","c"' | sed 's/^,//'

For file:

sed -i.bak 's/^,//' infile.log
+1
source

Source: https://habr.com/ru/post/1651493/


All Articles