LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-15-2022, 10:06 AM   #1
salmanahmed
Member
 
Registered: Jun 2020
Posts: 158

Rep: Reputation: Disabled
sed - remove decimal points


Hi
I am using following script to replace certain numbers in my text files.
Code:
#!/bin/bash
sed -i -e 's/2.66666666666667/2/' -i -e 's/3.33333333333333/3/' \
        -i -e 's/4.66666666666667/4/' -i -e 's/5.33333333333333/5/' $1
echo ""
echo "Conversion completed for" $1
However, the disadvantage of this script is I have to enter all the numbers manually in the script which will result in unnecessarily and overwhelmingly large script.
Actually, the basic function which I want to achieve from this script is to remove all the numbers after the decimal point. For example, 2.667 should be converted to 2 and so on.
So, I think there must be a better way to write this script, instead of manually entering the numbers in the script.
Can you guide me towards the right-way to write this script? Thanks
 
Old 04-15-2022, 10:24 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,101

Rep: Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365
Code:
sed 's/\.[0-9]*//'
 
4 members found this post helpful.
Old 04-15-2022, 11:15 AM   #3
salmanahmed
Member
 
Registered: Jun 2020
Posts: 158

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
Code:
sed 's/\.[0-9]*//'
Thanks a lot. I wasn't expecting that the solution would be so simple :-)
 
Old 04-15-2022, 11:15 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,797

Rep: Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002
Quote:
Originally Posted by salmanahmed View Post
Hi
I am using following script to replace certain numbers in my text files.
Code:
#!/bin/bash
sed -i -e 's/2.66666666666667/2/' -i -e 's/3.33333333333333/3/' \
        -i -e 's/4.66666666666667/4/' -i -e 's/5.33333333333333/5/' $1
echo ""
echo "Conversion completed for" $1
However, the disadvantage of this script is I have to enter all the numbers manually in the script which will result in unnecessarily and overwhelmingly large script. Actually, the basic function which I want to achieve from this script is to remove all the numbers after the decimal point. For example, 2.667 should be converted to 2 and so on. So, I think there must be a better way to write this script, instead of manually entering the numbers in the script. Can you guide me towards the right-way to write this script?
pan64 gave a great answer, but I'll ask the question of "why?" How are these numbers getting in to whatever you're 'converting' in the first place?? Be far simpler to do the conversion there, versus later on. And rounding 2.667 to 2 would be rounding down...wouldn't 2.6 round UP to 3?
 
1 members found this post helpful.
Old 04-15-2022, 11:29 AM   #5
salmanahmed
Member
 
Registered: Jun 2020
Posts: 158

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
pan64 gave a great answer, but I'll ask the question of "why?" How are these numbers getting in to whatever you're 'converting' in the first place?? Be far simpler to do the conversion there, versus later on. And rounding 2.667 to 2 would be rounding down...wouldn't 2.6 round UP to 3?
The purpose here is just to remove the portion after decimal. It is nothing to do with rounding.
 
Old 04-15-2022, 11:42 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,797

Rep: Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002
Quote:
Originally Posted by salmanahmed View Post
The purpose here is just to remove the portion after decimal. It is nothing to do with rounding.
Gotcha....still, it would seem to make more sense to have whatever is outputting these numbers print things the way you want, versus cleaning it up after.
 
Old 04-15-2022, 12:11 PM   #7
salmanahmed
Member
 
Registered: Jun 2020
Posts: 158

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Gotcha....still, it would seem to make more sense to have whatever is outputting these numbers print things the way you want, versus cleaning it up after.
The output is being created by Libreoffice calc. I know that we can adjust the output in Cell Properties > Number > Decimal.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
4gig file trying to replace decimal number A with decimal number B tasdca Linux - Software 5 03-26-2015 05:51 PM
How to convert last digit to decimal with sed (274 to 27,4) junkyhlm Linux - General 2 03-22-2013 04:43 AM
[SOLVED] BC provides incorrect decimal results, but only on the last two decimal places... standard_output Linux - Newbie 4 06-27-2012 05:30 PM
play free (World of WarCraft , xBox Live, Live Points, Wii Points, Free Habbo) laraaj Linux - Games 1 02-09-2007 05:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration