0

I have a csv as follows

1.2kbps,link1,description
1.2mbps,link2,description

I would like to Find and replace

a)dot and kbps with 000 in column1

b)dot and mbps with 000000 in column1 using gawk or awk and output should look like follows

12000,link1,description
12000000,link2,description

I prefer Gawk but can use awk examples and play with it

Thanks

steve
  • 21,892
  • Are you guaranteed to have only one digit past the decimal? Also, the number of 0's in both your examples is wrong. – Alex Jul 03 '17 at 21:44
  • A rate of 1.2kbps should be 1200 (not 12000, one zero more than needed). –  Jul 03 '17 at 22:36
  • There is a possibility of 1.24kbps or 1.2345mbps in the data set..I just need the dot to be replaced with correct number of zeroes to normalize the data set. – Programmingnewbie Jul 04 '17 at 01:54

2 Answers2

0

Try this (tested against GNU Awk 3.1.7):

awk '{sub(/\./,"");sub(/kbps/,"000");sub(/mbps/,"000000")}1' foo.csv
steve
  • 21,892
0

This should solve your problem:

awk -F, '
         BEGIN{OFS=FS}
         /kbps/{gsub("kbps","",$1);$1=$1*1000};
         /mbps/{gsub("mbps","",$1);$1=$1*1000000}
         1' file.csv
  • Thanks...Is the Gnuwin32 gawk syntax different than the above?..I am getting syntax error. "awk: '{sub(/./,);sub(/kbps/,000);sub(/mbps/,000000)}1' awk: ^ invalid char ''' in expression" – Programmingnewbie Jul 04 '17 at 03:26
  • @Programmingnewbie Maybe that is a comment to make in the other answer. And, maybe, you should not place the command inside "double quotes". –  Jul 04 '17 at 04:15