Hello,
I have a comman separated file lets day data.txt in following format
,:000002 CH XIN9I.INDX, 34.7534909645,:000002 CH,:Index XIN9I.INDX
,:000063 CH XIN9I.INDX, 6.3062924781,:000063 CH,:Index XIN9I.INDX
,:000776 CH XIN9I.INDX, 2.7001954832,:000776 CH,:Index XIN9I.INDX
I would like to replace all occurences of [0-9][0-9][0-9][0-9][0-9][0-9] CH with [0-9][0-9][0-9][0-9][0-9][0-9].CH
i.e. following
:000002.CH XIN9I.INDX, 34.7534909645,:000002.CH,:Index XIN9I.INDX
,:000063.CH XIN9I.INDX, 6.3062924781,:000063.CH,:Index XIN9I.INDX
,:000776.CH XIN9I.INDX, 2.7001954832,:000776.CH,:Index XIN9I.INDX
I tried
cat data.txt | sed "s/[0-9][0-9][0-9][0-9][0-9][0-9] CH/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].CH/g" > out.txt
and it did not work as expected.
I would really appreciate any help.
try this,
sed 's/\(......\) \+CH/\1\.CH/g' infile
sed 's/\([0-9]\{6\}\) \+CH/\1\.CH/g' infile
OR
perl -pe 's/(.+?)\s+CH/$1.CH/g' infile
Another one:
sed 's/\([0-9]\{6\}\) /\1./g' file
I tried it, the out put is unchanged, please advise, I would really appreciate it
ctsgnb
5
Or just ... (if apply .... depending on the rest of the file)
sed 's/ CH/\.CH/g' infile
?
Hi ctsgnb,
Thank you so much it worked, m I reading it correctly that you ares suggesting to replace space CH with dot CH, correct?
Thanks
---------- Post updated at 01:15 PM ---------- Previous update was at 01:13 PM ----------
Now when I think of it, I believe we would want it to be stricter than that
There will be exactly 6-digits then space and CH and requirement is to replace it with 6-digits then dot CH
Please advise, I would appreciate it
If you're referring to pravin27's sed suggestions, it could be because they use a gnu extension (\+) and you are not using gnu sed.
Regards,
Alister
---------- Post updated at 02:19 PM ---------- Previous update was at 02:17 PM ----------
Try:
sed 's/\([[:digit:]]\{6\}\) CH/\1.CH/g'
Regards,
Alister
1 Like
Hi Franklin and ctsgnb,
I modified the version Franklin send and made some changes and modified it to
sed 's/\([0-9]\{6\} CH/\1.CH/g' data.txt
And it worked, thanks.
Thanks to everyone who replied, you guys are life savers.
ctsgnb
9
+1 for alister's suggestion
kurumi
10
Ruby
$ ruby -ne 'puts $_.gsub(/:(\d{6})\s+(CH)/,"\\1.\\2")' file
,:000002.CH XIN9I.INDX, 34.7534909645,:000002.CH,:Index XIN9I.INDX
,:000063.CH XIN9I.INDX, 6.3062924781,:000063.CH,:Index XIN9I.INDX
,:000776.CH XIN9I.INDX, 2.7001954832,:000776.CH,:Index XIN9I.INDX