Add Delimiter after 2 decimal point for a particular column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add Delimiter after 2 decimal point for a particular column
# 1  
Old 04-03-2017
Add Delimiter after 2 decimal point for a particular column

Hi All,
Please help for the following scenario where I want to add a delimiter after 2 decimal point integer.
Code:
Original Line
1007937820§L§2016-12-19§000000002§2018-02-01§2050-12-01§00395§M§146713.57§00005.05000§762.59§00395§M§301223.0528§1165§2017-03-31 20:34:25

Expected Line
1007937820§L§2016-12-19§000000002§2018-02-01§2050-12-01§00395§M§146713.57§00005.05000§762.59§00395§M§301223.05§28§1165§2017-03-31 20:34:25

After 2 decimal point I would like to add a delimiter "§", Above is just a single line, in actual it contains 17K rows. This need to apply the same to all rows.

Thanks.
# 2  
Old 04-03-2017
I was afraid that § being a two byte character would make things difficult, but (at least my) sed (sed (GNU sed) 4.2.2) seems to be locale aware. Try
Code:
sed 's/\(\.[0-9][0-9]\)\([^§]\)/\1§\2/g' file
1007937820§L§2016-12-19§000000002§2018-02-01§2050-12-01§00395§M§146713.57§00005.05§000§762.59§00395§M§301223.05§28§1165§2017-03-31 20:34:25

Why, BTW, is 00005.05 NOT modified in your output sample?

Last edited by RudiC; 04-03-2017 at 10:13 AM..
# 3  
Old 04-03-2017
Using perl's negative lookbehind:
Code:
perl -pe 's/\.\d\d(?!§)/$&§/g' file

# 4  
Old 04-03-2017
Thanks Guy. I will reply the outcome after I tested.
# 5  
Old 04-03-2017
Quote:
Originally Posted by Scrutinizer
Using perl's negative lookbehind:
Code:
perl -pe 's/\.\d\d(?!§)/$&§/g' file

That's the negative lookahead assertion.
The negative lookbehind assertion would look like this:
Code:
(?<!regex)

# 6  
Old 04-03-2017
Thank You Guys,
However perl and sed solution provided will replace the first occurrence as well.
I found Rudic given sed solution has included 2nd occurrence, but it still change for the first.
I wish can see how perl to resolve this as well.
Code:
1007937820§L§2016-12-19§000000002§2018-02-01§2050-12-01§00395§M§146713.57§00005.05000§762.59§00395§M§301223.0528§1165§2017-03-31 20:34:25

1007937820§L§2016-12-19§000000002§2018-02-01§2050-12-01§00395§M§146713.57§00005.05§000§762.59§00395§M§301223.05§28§1165§2017-03-31 20:34:25

---------- Post updated at 09:05 PM ---------- Previous update was at 08:20 PM ----------

Hi Guys,
manage to get it resolve by minor change on the occurrence, instead of /g use /2 on the sed command we use.
Code:
  sed 's/\(\.[0-9][0-9]\)\([^§]\)/\1§\2/2'

  1007937820§L§2016-12-19§000000002§2018-02-01§2050-12-01§00395§M§146713.57§00005.05000§762.59§00395§M§301223.05§28§1165§2017-03-31 20:34:25

Hopes we can also know how it can be achieve by using perl command too.

Thanks So much you all the contribution.
# 7  
Old 04-04-2017
Indeed, that (non-) modification of the first occurrence was NOT specified in the first place, and I asked about what to do...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like A|Mau|Code|12|Detail B|Mau|Code|20|Header I want to write a command using awk which will output A|Mau|Code|12.00|Detail B|Mau|Code|20.00|Header I used a command like awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}' which does the... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. UNIX for Beginners Questions & Answers

How to add following decimal point to a CSV value?

hi there I being trying to figure out way to add " .0" to an integer value in a csv using sed or awk with out success. just as a work around for 2147483647 32 bit limitation that influxdb is currently having the data base will accept values and work fine if it has the XXX.0 ... (7 Replies)
Discussion started by: sash99
7 Replies

3. Shell Programming and Scripting

Help with Round Up with 2 decimal point at specific column

Input file: USA 20.5683 UK 3.54221 Japan 2.54001 China 2.50897 Germany 2.05816 . . Desired output file: USA 20.57 UK 3.54 Japan 2.54 China 2.51 Germany 2.06 . . (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. Shell Programming and Scripting

Check for decimal point and add it at the end if its not there using awk/perl

I have test.dat file with values given below: 20150202,abc,,,,3625.300000,,,,,-5,,,,,,,,,,,,,,,,,,,,,, 20150202,def,,,,32.585,,,,,0,,,,,,,,,,,,,,,,,,,,,, 20150202,xyz,,,,12,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,, My expected output is shown below: ... (1 Reply)
Discussion started by: nvk_vinoth
1 Replies

5. Shell Programming and Scripting

awk decimal point numbers matching

Hi All, Can some one help me in identifying the significance of character "$" ,Which is playing critical role in matching decimal point numbers as below. $ echo "01#.01"|awk '{if ($0 ~ /^+(\.*)?$/) print}' $ echo "01#.01"|awk '{if ($0 ~ /^+(\.*)?/) print}' 01#.01 $ Regards, Rmkganesh. (3 Replies)
Discussion started by: rmkganesh
3 Replies

6. Shell Programming and Scripting

remove directories with two digits after decimal point

Hi everyone, I am new here and generally not experienced with linux. My question must be easy, but as for now I have no idea how to do it. I have lots of directories with numerical names, e.g. 50 50.1 50.12 etc. What I want is to leave directories with no or single digit after the decimal... (2 Replies)
Discussion started by: cabaciucia
2 Replies

7. Shell Programming and Scripting

Insert decimal point for numbers

Hi In Unix, I have a file with some numbers like : 45600 12345 I want to insert a decimal point for these numbers based on user input. If the input is 2, the numbers should be changed to 456.00 123.45 If the input is 3, the numbers should be changed to 45.600 12.345 Can... (2 Replies)
Discussion started by: yoursdivu
2 Replies

8. Shell Programming and Scripting

Comparing two numbers with decimal point

How to compare two numbers with decimal points ? Is there a way in bash to do this? (33 Replies)
Discussion started by: kinny
33 Replies

9. Shell Programming and Scripting

Insert a decimal point

Hi all. Using /bin/sh on an HPUX system. I want to place a decimal in the field 2 charactors from the right (yes, converting to currency). The field lengths are variable. Here's what I'm doing: exec < filename while read FIELD1 FIELD2 do FIELD1="echo $FIELD1 | sed 'syntax that will... (4 Replies)
Discussion started by: lyoncc
4 Replies

10. Shell Programming and Scripting

how to get rid of decimal point?

Hi, I have input with decimal point ( 9.99 ) for hours variable hrs. I need to change it to seconds. Here is my code: secs=`/usr/ucb/echo $hrs*3600 |bc` But I don't want to see the decimal point. I can use awk to trim it if there is one. I am just wondering if there is better standard... (2 Replies)
Discussion started by: cin2000
2 Replies
Login or Register to Ask a Question