I have a huge CSV file which contains 100000 rows. However, I need to do some processing and so for that I need to have a single space before the first comma. My current data is something like below.


I need a space before the first comma for some internal processing. So, after the command execution, I need the file to be like below.

123456 ,5,4,3,2,1

I tried the gawk command to get the first column alone using $1. However, $1 gives me the entire row. Is there another way to do the above task?

  • Can you please clarify whether you really meant "space before the first comma", since Stephane changed it? – Chris Down Nov 5 '13 at 11:09
  • Yeah, I needed space before first comma. However, I got the idea from sed command. Thanks agaiN! – Ramesh Nov 5 '13 at 15:03

You can do this quite easily with sed using sed 's/,/ ,/'.

$ sed 's/,/ ,/' <<< 123456,5,4,3,2,1
123456 ,5,4,3,2,1

Without the "g" modifier, sed only acts on the first match on a line (with "g", it acts on all of them).

  • I am using the command as below. sed 's/,/ ,/' <<< file.csv >> file1.csv But however, I get only file1.csv inside the file1.csv as the content. What am I doing wrong? – Ramesh Nov 5 '13 at 5:56
  • AAh, figured out. I don't need the <<< in the input file specification. Thanks a lot! – Ramesh Nov 5 '13 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.