简体   繁体   English

从bash中的csv文件中删除第一个逗号

[英]Remove first comma from a csv file in bash

I have a csv file in which some lines start with a comma, so I want to remove all of them. 我有一个csv文件,其中一些行以逗号开头,所以我想删除所有这些。

Example: In this line: ,"a","b","c" , I want to remove the first comma. 示例:在这一行: ,"a","b","c" ,我想删除第一个逗号。

How do I do this in bash? 我怎么在bash中这样做?

You can use this sed : 你可以使用这个sed

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

^[[:blank:]]*, will match comma at line start with optional whitespaces before comma. ^[[:blank:]]*,将逗号与逗号之前的可选空格匹配。

try this; 试试这个;

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

Ex; 防爆;

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

Do not use "g" flag in sed, it will help you in removing only first matching "," 不要在sed中使用“g”标志,它将帮助您仅删除第一个匹配的“,”

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

For file : 对于文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM