Changing every other comma to point


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing every other comma to point
# 1  
Old 07-14-2016
Changing every other comma to point

Hi

I am working with set of data which is written in Swedish format. comma is used instead of point for decimal numbers in Sweden.

My data set is like this:

Code:
1,188,1,250,0,757,0,946,8,960
1,257,1,300,0,802,1,002,9,485
1,328,1,350,0,846,1,058,10,021
1,381,1,400,0,880,1,100,10,418

Which I want to change every other comma to point and have output like this:

Code:
1.188,1.250,0.757,0.946,8.960
1.257,1.300,0.802,1.002,9.485
1.328,1.350,0.846,1.058,10.021
1.381,1.400,0.880,1.100,10.418

Any idea of how to do that with simple shell scripting. It is fine If I do it in multiple steps. I mean if I change first the first instance of comma and then the third instance and ...

Thank you very much for your help.

Last edited by Johanni; 07-14-2016 at 05:21 AM.. Reason: Code
# 2  
Old 07-14-2016
Hello Johanni,

Please use code tags as per forum rules for your codes/Inputs/commands into your posts.
Could you please try following and let me know if this helps.
Code:
awk '{num=split($0,A,",");for(i=1;i<=num;i++){Q=i%2==0?".":",";VAL=VAL?VAL Q A[i]:A[i]};print VAL;VAL=""}'   Input_file

Output will be as follows.
Code:
1.188,1.250,0.757,0.946,8.960
1.257,1.300,0.802,1.002,9.485
1.328,1.350,0.846,1.058,10.021
1.381,1.400,0.880,1.100,10.418

Thanks,
R. Singh
# 3  
Old 07-14-2016
It worked great, the only problem is it would removes zero if it is in the begining of the line. example of you script performance:

input:
Code:
0,003,0,200,0,000,0,000,0,020

Output:
Code:
003,0.200,0.000,0.000,0.020

# 4  
Old 07-14-2016
Try
Code:
sed 's/$/,/; s/\([^,]*\),\([^,]*,\)/\1.\2/g; s/,$//' file
1.188,1.250,0.757,0.946,8.960
1.257,1.300,0.802,1.002,9.485
1.328,1.350,0.846,1.058,10.021
1.381,1.400,0.880,1.100,10.418
0.003,0.200,0.000,0.000,0.020

This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-14-2016
Work 100% correct.
Thanks

Quote:
Originally Posted by RudiC
Try
Code:
sed 's/$/,/; s/\([^,]*\),\([^,]*,\)/\1.\2/g; s/,$//' file
1.188,1.250,0.757,0.946,8.960
1.257,1.300,0.802,1.002,9.485
1.328,1.350,0.846,1.058,10.021
1.381,1.400,0.880,1.100,10.418
0.003,0.200,0.000,0.000,0.020

# 6  
Old 07-14-2016
Code:
awk '{for (i=1; i<NF; i+=2) l=l $i "." $(i+1) FS; $0=l; NF=NF-1; l=""} 1' FS=, OFS=, infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file: EK ED,Elementary and... (7 Replies)
Discussion started by: tdouty
7 Replies

3. Shell Programming and Scripting

Replace comma and blank with comma and number

I, I have a file and i need to replace comma and blank space with comma and 0. cat file.txt a,5 b,1 c, d, e,4 I need the output as cat file.txt a,5 b,1 c,0 d,0 (4 Replies)
Discussion started by: jaituteja
4 Replies

4. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

5. Shell Programming and Scripting

How to grep after the first comma till the next comma in a line

Hi Can any one pls tell me how to grep this line POPULATION,69691,20120509 I want the number 69691 from the above line. How to grep from the first comma till the next comma. Thank You.:confused: (8 Replies)
Discussion started by: rxg
8 Replies

6. Shell Programming and Scripting

How to perform a hexdump using dd from start point to end point?

hi, I would like to ask or is it possible to dump a hex using dd from starting point to end point just like the "xxd -s 512 -l 512 <bin file>" I know the redirect hexdump -C but i can't figure it out the combination options of dd. Hope someone can share their knowledge.. Thanks in... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. Shell Programming and Scripting

Pull Data After Comma if 2 word before comma

Hi, I am trying to truncate word after comma in a file ONLY if there are already 2 words BEFORE comma. If there is one word or 3 or more words BEFORE comma, then I have to leave the data AS IS. See below for example. Input File : John Smith, Manager Smith, John Frank J F K... (2 Replies)
Discussion started by: msalam65
2 Replies

8. Solaris

Neat trick: Changing the permissions of an underlying mount point

A colleague of mine showed me a neat little trick in Solaris (I would guess sol 10 but perhaps earlier versions too) that I'd not seen before and thought I'd share here in case it's new for someone else also. As most of you know, Solaris has the annoying habit of producing error messages when... (6 Replies)
Discussion started by: Smiling Dragon
6 Replies

9. Programming

changing entry point

hi... just wanted to check how i can the entry point of a program... i tried using the #pragma directive but it doesnt seem to be workin... #pragma comment(linker,"/ENTRY:startupfunction") #include<stdio.h> void startupfunction() { printf("in print\n"); main(); } int main() {... (4 Replies)
Discussion started by: strider
4 Replies

10. UNIX for Advanced & Expert Users

Fibre connection Point to Point SUN

Anyone know of a guide or instructions for Solaris I got to configure a SBUS HBA to talk to a tape robot. I have done this on a switch but not point to point. just going HBA >>>>> TAPE Fibre simple two nodes Kie (6 Replies)
Discussion started by: kie
6 Replies
Login or Register to Ask a Question