CSV how to trim values to 2 places in multiple columns using UNIXre-arrange values in one column without affecting other columns using awk or sedUsing jq to extract values and format in CSVHow to Trim below highlighted lines from Shell outputConcatenate CSV with some shared columnsRemove specific columns from csv using awkhow to capture only the “csv” lines with 5 valuesPattern recognition and summing columns between two csv/excel filesHow do I use different replacement texts for a pattern found multiple timesParsing CSV that has multiple line values with carriage returnsHow to Merge Multiple Columns in to Two Columns based on Column 1 Value?

Multi tool use
How do I turn off a repeating trade?
Set multicolumn to a exact width
What are the penalties for overstaying in USA?
How would a drone work in centrifugal force generated "gravity"?
Does squid ink pasta bleed?
Can White Castle?
Unusual mail headers, evidence of an attempted attack. Have I been pwned?
Hand soldering SMD 1206 components
Swapping rooks in a 4x4 board
Should I prioritize my 401(k) over my student loans?
Why aren't (poly-)cotton tents more popular?
Is this one of the engines from the 9/11 aircraft?
STM Microcontroller burns every time
What is the origin of Scooby-Doo's name?
What reason would an alien civilization have for building a Dyson Sphere (or Swarm) if cheap Nuclear fusion is available?
If the world have massive single giant world tree can it stop earthquake?
Cascading Repair Costs following Blown Head Gasket on a 2004 Subaru Outback
Folding basket - is there such a thing?
What is the legal status of travelling with methadone in your carry-on?
Is adding a new player (or players) a DM decision, or a group decision?
Why do textbooks often include the solutions to odd or even numbered problems but not both?
Fill NAs in R with zero if the next valid data point is more than 2 intervals away
Why do some professors with PhDs leave their professorships to teach high school?
Has there been any indication at all that further negotiation between the UK and EU is possible?
CSV how to trim values to 2 places in multiple columns using UNIX
re-arrange values in one column without affecting other columns using awk or sedUsing jq to extract values and format in CSVHow to Trim below highlighted lines from Shell outputConcatenate CSV with some shared columnsRemove specific columns from csv using awkhow to capture only the “csv” lines with 5 valuesPattern recognition and summing columns between two csv/excel filesHow do I use different replacement texts for a pattern found multiple timesParsing CSV that has multiple line values with carriage returnsHow to Merge Multiple Columns in to Two Columns based on Column 1 Value?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
The sample file structure is given below
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.999999,1.409999,15.9988999
Biscuit,2.999999,2.409999,15.9988999
The output file is expected to be in the format below:
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
text-processing sed csv
add a comment |
The sample file structure is given below
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.999999,1.409999,15.9988999
Biscuit,2.999999,2.409999,15.9988999
The output file is expected to be in the format below:
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
text-processing sed csv
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
Jun 6 at 13:09
add a comment |
The sample file structure is given below
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.999999,1.409999,15.9988999
Biscuit,2.999999,2.409999,15.9988999
The output file is expected to be in the format below:
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
text-processing sed csv
The sample file structure is given below
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.999999,1.409999,15.9988999
Biscuit,2.999999,2.409999,15.9988999
The output file is expected to be in the format below:
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
text-processing sed csv
text-processing sed csv
edited Jun 6 at 16:47
K7AAY
2,0951 gold badge10 silver badges29 bronze badges
2,0951 gold badge10 silver badges29 bronze badges
asked Jun 6 at 13:03
AshishAshish
161 bronze badge
161 bronze badge
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
Jun 6 at 13:09
add a comment |
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
Jun 6 at 13:09
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
Jun 6 at 13:09
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
Jun 6 at 13:09
add a comment |
3 Answers
3
active
oldest
votes
On systems with GNU Coreutils, you might consider using numfmt
e.g.
$ numfmt --delimiter=, --header --field=2- --format='%.2f' --round=down < file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
If you want conventional IEEE from-zero rounding, omit the --round=down
directive.
add a comment |
This does what you asked for:
$ sed -E 's/([0-9]+.[0-9]2)[0-9]*/1/g' file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
Or, if you want to round the numbers instead of just removing the extra digits, you could try:
$ perl -pe 's/(d+.d+)/sprintf("%0.2f",$1)/ge' file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,2.00,1.41,16.00
Biscuit,3.00,2.41,16.00
This works for me. Thanks a lot everyone for help.
– Ashish
Jun 7 at 6:02
add a comment |
awk -F "," 'NR>1print $1,$2,$3,$4' y.txt |awk 'print $1,substr($2,1,4),substr($3,1,4),substr($4,1,5)'|awk 'OFS="," print $1,$2,$3,$41' filename
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).
– Jeff Schaller♦
Jun 6 at 18:56
This seems overly complicated, not to mention that the lastawk
is given an input file and ignores the pipe.
– RalfFriedl
Jun 6 at 20:02
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523314%2fcsv-how-to-trim-values-to-2-places-in-multiple-columns-using-unix%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
On systems with GNU Coreutils, you might consider using numfmt
e.g.
$ numfmt --delimiter=, --header --field=2- --format='%.2f' --round=down < file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
If you want conventional IEEE from-zero rounding, omit the --round=down
directive.
add a comment |
On systems with GNU Coreutils, you might consider using numfmt
e.g.
$ numfmt --delimiter=, --header --field=2- --format='%.2f' --round=down < file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
If you want conventional IEEE from-zero rounding, omit the --round=down
directive.
add a comment |
On systems with GNU Coreutils, you might consider using numfmt
e.g.
$ numfmt --delimiter=, --header --field=2- --format='%.2f' --round=down < file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
If you want conventional IEEE from-zero rounding, omit the --round=down
directive.
On systems with GNU Coreutils, you might consider using numfmt
e.g.
$ numfmt --delimiter=, --header --field=2- --format='%.2f' --round=down < file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
If you want conventional IEEE from-zero rounding, omit the --round=down
directive.
answered Jun 6 at 13:27
steeldriversteeldriver
40k4 gold badges54 silver badges93 bronze badges
40k4 gold badges54 silver badges93 bronze badges
add a comment |
add a comment |
This does what you asked for:
$ sed -E 's/([0-9]+.[0-9]2)[0-9]*/1/g' file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
Or, if you want to round the numbers instead of just removing the extra digits, you could try:
$ perl -pe 's/(d+.d+)/sprintf("%0.2f",$1)/ge' file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,2.00,1.41,16.00
Biscuit,3.00,2.41,16.00
This works for me. Thanks a lot everyone for help.
– Ashish
Jun 7 at 6:02
add a comment |
This does what you asked for:
$ sed -E 's/([0-9]+.[0-9]2)[0-9]*/1/g' file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
Or, if you want to round the numbers instead of just removing the extra digits, you could try:
$ perl -pe 's/(d+.d+)/sprintf("%0.2f",$1)/ge' file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,2.00,1.41,16.00
Biscuit,3.00,2.41,16.00
This works for me. Thanks a lot everyone for help.
– Ashish
Jun 7 at 6:02
add a comment |
This does what you asked for:
$ sed -E 's/([0-9]+.[0-9]2)[0-9]*/1/g' file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
Or, if you want to round the numbers instead of just removing the extra digits, you could try:
$ perl -pe 's/(d+.d+)/sprintf("%0.2f",$1)/ge' file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,2.00,1.41,16.00
Biscuit,3.00,2.41,16.00
This does what you asked for:
$ sed -E 's/([0-9]+.[0-9]2)[0-9]*/1/g' file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
Or, if you want to round the numbers instead of just removing the extra digits, you could try:
$ perl -pe 's/(d+.d+)/sprintf("%0.2f",$1)/ge' file.csv
Product,Base_Price,Promotion_Price,Discount
Shampoo,2.00,1.41,16.00
Biscuit,3.00,2.41,16.00
answered Jun 6 at 13:14
terdon♦terdon
137k33 gold badges283 silver badges459 bronze badges
137k33 gold badges283 silver badges459 bronze badges
This works for me. Thanks a lot everyone for help.
– Ashish
Jun 7 at 6:02
add a comment |
This works for me. Thanks a lot everyone for help.
– Ashish
Jun 7 at 6:02
This works for me. Thanks a lot everyone for help.
– Ashish
Jun 7 at 6:02
This works for me. Thanks a lot everyone for help.
– Ashish
Jun 7 at 6:02
add a comment |
awk -F "," 'NR>1print $1,$2,$3,$4' y.txt |awk 'print $1,substr($2,1,4),substr($3,1,4),substr($4,1,5)'|awk 'OFS="," print $1,$2,$3,$41' filename
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).
– Jeff Schaller♦
Jun 6 at 18:56
This seems overly complicated, not to mention that the lastawk
is given an input file and ignores the pipe.
– RalfFriedl
Jun 6 at 20:02
add a comment |
awk -F "," 'NR>1print $1,$2,$3,$4' y.txt |awk 'print $1,substr($2,1,4),substr($3,1,4),substr($4,1,5)'|awk 'OFS="," print $1,$2,$3,$41' filename
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).
– Jeff Schaller♦
Jun 6 at 18:56
This seems overly complicated, not to mention that the lastawk
is given an input file and ignores the pipe.
– RalfFriedl
Jun 6 at 20:02
add a comment |
awk -F "," 'NR>1print $1,$2,$3,$4' y.txt |awk 'print $1,substr($2,1,4),substr($3,1,4),substr($4,1,5)'|awk 'OFS="," print $1,$2,$3,$41' filename
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
awk -F "," 'NR>1print $1,$2,$3,$4' y.txt |awk 'print $1,substr($2,1,4),substr($3,1,4),substr($4,1,5)'|awk 'OFS="," print $1,$2,$3,$41' filename
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99
answered Jun 6 at 18:22
Praveen Kumar BSPraveen Kumar BS
2,1282 gold badges3 silver badges11 bronze badges
2,1282 gold badges3 silver badges11 bronze badges
I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).
– Jeff Schaller♦
Jun 6 at 18:56
This seems overly complicated, not to mention that the lastawk
is given an input file and ignores the pipe.
– RalfFriedl
Jun 6 at 20:02
add a comment |
I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).
– Jeff Schaller♦
Jun 6 at 18:56
This seems overly complicated, not to mention that the lastawk
is given an input file and ignores the pipe.
– RalfFriedl
Jun 6 at 20:02
I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).
– Jeff Schaller♦
Jun 6 at 18:56
I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).
– Jeff Schaller♦
Jun 6 at 18:56
This seems overly complicated, not to mention that the last
awk
is given an input file and ignores the pipe.– RalfFriedl
Jun 6 at 20:02
This seems overly complicated, not to mention that the last
awk
is given an input file and ignores the pipe.– RalfFriedl
Jun 6 at 20:02
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523314%2fcsv-how-to-trim-values-to-2-places-in-multiple-columns-using-unix%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
p9ipL W1AydHuAYVqxOozHKBhhYM Pu2bGy31 Py tsMHM5aU4H1HiA RnXBXbRgRxwp6,tVtgU Qo cnauNmLTcfaaIvcck,r
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
Jun 6 at 13:09