I want to remove first/last comma of multiple commas which are in between semicolon.
clarification: I have a comma separated key:value records in a text file which i want them to load into a db. Out of these records I have certain values which are separated by a comma as shown below. So, before i trim off key's from each record i want to remove "," which is in between firstname and lastname.
Note: All the names are not of same format meaning with first name and second name, some records are with only name.
Current format:
SNO:01, ID:123, Name: firstname, lastname, fname: firstname , lastname, sex:male, age:42
SNO:02, ID:124, Name: name, fname: firstname , lastname, sex:male, age:40
SNO:03, ID:125, Name: firstname , lastname, fname: name, sex:male, age:37
SNO:04, ID:126, Name: name, fname: name, sex:male, age:35
Required format:
SNO:01, ID:123, Name: firstname+lastname, fname: firstname+lastname, sex:male, age:42
SNO:02, ID:124, Name: name, fname: firstname+lastname, sex:male, age:40
SNO:03, ID:125, Name: firstname+lastname, fname: name, sex:male, age:37
SNO:04, ID:126, Name: name, fname: name, sex:male, age:35
I want firstname and lastname to be as just name. To achieve this i want to make sure there is only one comma in between two ":".
Thanks.
答案 0 :(得分:0)
你可以试试这个awk。
awk -F: -vOFS=: '{for(i=1;i<=NF;i++)if($i ~ /[^,]*,[^,]*,.*/)sub(",","+",$i)}1' infile
字段分隔符= :,输出字段分隔符=:
对于每个字段,请查找2个逗号。
如果找到,请用+
替换第一个1用于打印该行。