How to change comma delimeter in the file to number?

I have a file

H,20180624200732,VPAS,TRANS_HDR,20180724,
VPAS.TRANS_HDR.20180724.01.txt, ,93,
T,1,

I have to change and instead first comma put ",1" like below

H,20180624200732,VPAS,TRANS_HDR,20180724,
VPAS.TRANS_HDR.20180724.01.txt,1,93,
T,1,

I made

sed "2s/, /,1/" $CONTROLFILE

and it put nothing to the file
I tried

sed "2s/, /,1/" $CONTROLFILE > $CONTROLFILE 

In this case the file is empty

Please advise what can I do to have "....,1,...." in the file in second line

Everything I do from the shell script. let me know if I have to put all source code

Thanks for contribution

Hello digioleg54,

Could you please try following and let me know if this helps you.(This should substitute first occurrence of , in 2nd line with ,1, )

 sed '2s/, ,/,1,/1'  Input_file
 

In case you want to save output into Input_file itself use sed -i option in above code OR if you want to take backup of Input_file before saving output into it then use sed -i.bak in above code too.

Thanks,
R. Singh

Above mentioned -i option is not working

sed -i '2s/, ,/,1,/1' $CONTROLFILE

gives error below

sed: 1: "VPAS.TRANS_HDR.20180724 ...": invalid command code V
 

I am working on iMac terminal. The value of $CONTROLFILE = VPAS.TRANS_HDR.20180724.01.ctl

Thanks for contribution

Issues like this are why you should always tell us what operating system and shell you're using when you start a new thread. The code Ravinder suggested will work perfectly well on many systems, but since the -i option to sed is not required by the standards, it doesn't work on all systems.

The following should work on any standards conforming system if you're using a shell that conforms to the standards...

tmpf=./${0##*/}.$$

sed '2s/, /,1/' "$CONTROLFILE" > "$tmpf" && mv "$tmpf" "$CONTROLFILE"
rm -f "$tmpf"

Sorry, But I said that option -i is not working for me.

In your line the ksh shell script doesn't understand "mv" command.

Any advise on this?

Thanks for contribution

---------- Post updated at 09:42 PM ---------- Previous update was at 09:14 PM ----------

OK, I changed

"mv" to 
mv $tmpf $CONTROLFILE

Now ot works.

Thanks a lot for your help

Thanks a lot for your help
[/quote]

I have no idea what you're talking about!

The code I gave you didn't have mv in double quotes.

If you're saying you removed double quotes from the arguments to mv as in:

mv $tmpf $CONTROLFILE

instead of:

mv "$tmpf" "$CONTROLFILE"

you have made the code much less safe. And, making it less safe has absolutely nothing to do with ksh finding mv !!!

One might guess that you're using a WYSIWYG pretty-printing editor of some sort instead of using a text editor that uses ASCII letters and quoting characters that are required when producing shell scripts.

Please do the following:

  1. Tell us WHAT operating system (name and release number) are you using?
  2. Tell us WHAT shell (name and version) are you using?
  3. Tell us WHAT editor are you using to produce your shell script?
  4. Show us the exact text that is included in your shell script (in CODE tags)!
1 Like

And do so in each and every NEW thread!

1 Like

1.Darwin Olegs-iMac.local 17.6.0 Darwin Kernel Version 17.6.0: Tue May 8 15:22:16 PDT 2018; root:xnu-4570.61.1~1/RELEASE_X86_64 x86_64
MacOS High Sierra
2.ksh is /bin/ksh
3.vi
4. Script is very huge I don't know if I can put it here.
The program will be working on Linux, I don't know details, it is not my comany

Thanks for contribution

The code that I suggested in post #4 in this thread works exactly as you requested on a MacBook Pro running the same operating system that you are running on your iMac and you should have the same utilities on your iMac. I have absolutely no idea why it is working for me and fails for you if you copied the code I suggested without modifying it.