-1

I have a text file (approx. 300'000 lines) with a unique column exhibiting 2 to 6 fields separated by a comma.

A,BB,CC  
EF,GHI,DKLM,OP          
Q,RS,TUV 
W,XY 

What I would I like to achieve:

A,B,C  
EF,HI,KLM,P         
Q,S,UV 
W,Y 

Said differently, I would like to remove in each line, the FIRST character after each comma.

Thanks in advance for your time and help. Best regards,

Laurent

1 Answers1

3
$ cat file
A,BB,CC
EF,GHI,DKLM,OP
Q,RS,TUV
W,XY
$ sed 's/,./,/g' file
A,B,C
EF,HI,KLM,P
Q,S,UV
W,Y

The regular expression ,. matches a literal comma followed by any other character whatsoever. The sed expression s/,./,/g substitutes each comma and the following character with just a single comma, effectively removing that other character. The substitution is carried out for each non-overlapping match on every line.

Redirect the output to a new file, or use sed with -i in an appropriate way (see How can I achieve portability with sed -i (in-place editing)?).

Kusalananda
  • 333,661
  • Dear Kusalananda. Thanks for your response, I appreciate that you took some of your time to help me. I have a question following your post. What do you mean by "The substitution is carried out for each non-overlapping match on every line"? If in a same line, I have twice the same substitution, for instance [,X] and later on the same line [,X], it will not work? – Opus_francigenum Jul 19 '21 at 10:15
  • 1
    @Opus_francigenum In your example, there is never any problem since all occurrences of ,X are non-overlapping. If you had ,,X, then the second comma would be removed, not the X. This is because in ,,X, the two substrings ,, and ,X are overlapping. If you have cases like this and want to avoid removing commas, then use s/,[^,]/,/g instead as the sed expression. This changes the meaning from "change any comma and the following character with a comma" to "replace any comma and the following non-comma with a comma". – Kusalananda Jul 19 '21 at 10:22
  • Crystal clear. Many thanks Kusalananda for your time ... and patience :-) Best regards to you. – Opus_francigenum Jul 19 '21 at 10:35
  • @Opus_francigenum Good! If this solves your issue, please consider accepting the answer. Accepting an answer marks the issue as resolved. – Kusalananda Jul 19 '21 at 10:38