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?
bmatrix: how to align elements' subscripts?
How to safely destroy (a large quantity of) valid checks?
Why can my keyboard only digest 6 keypresses at a time?
How can I get an unreasonable manager to approve time off?
US doctor working in Tripoli wants me to open online account
Why are trash cans referred to as "zafacón" in Puerto Rico?
Why can I traceroute to this IP address, but not ping?
Longest bridge/tunnel that can be cycled over/through?
Why was this person allowed to become Grand Maester?
How can I end combat quickly when the outcome is inevitable?
What ways have you found to get edits from non-LaTeX users?
If I leave the US through an airport, do I have to return through the same airport?
Should I ask for an extra raise?
Let M and N be single-digit integers. If the product 2M5 x 13N is divisible by 36, how many ordered pairs (M,N) are possible?
Is it legal for a bar bouncer to confiscate a fake ID
Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?
sed + add word before string only if not exists
Who enforces MPAA rating adherence?
Who won a Game of Bar Dice?
A strange trigonometric identity
Is using 'echo' to display attacker-controlled data on the terminal dangerous?
How to hide an urban landmark?
Interval of parallel 5ths in the resolution of a German 6th chord
ed command: Delete from line 1 until the first blank line
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
New contributor
Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
New contributor
Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
9 hours ago
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
New contributor
Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
New contributor
Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 6 hours ago
K7AAY
1,8731029
1,8731029
New contributor
Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 9 hours ago
AshishAshish
161
161
New contributor
Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
9 hours ago
add a comment |
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
9 hours ago
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
9 hours ago
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
9 hours ago
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
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♦
3 hours ago
This seems overly complicated, not to mention that the lastawkis given an input file and ignores the pipe.
– RalfFriedl
2 hours ago
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
);
);
Ashish is a new contributor. Be nice, and check out our Code of Conduct.
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 9 hours ago
steeldriversteeldriver
39.6k45492
39.6k45492
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
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
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 9 hours ago
terdon♦terdon
136k33276457
136k33276457
add a comment |
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♦
3 hours ago
This seems overly complicated, not to mention that the lastawkis given an input file and ignores the pipe.
– RalfFriedl
2 hours ago
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♦
3 hours ago
This seems overly complicated, not to mention that the lastawkis given an input file and ignores the pipe.
– RalfFriedl
2 hours ago
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 4 hours ago
Praveen Kumar BSPraveen Kumar BS
2,0432311
2,0432311
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♦
3 hours ago
This seems overly complicated, not to mention that the lastawkis given an input file and ignores the pipe.
– RalfFriedl
2 hours ago
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♦
3 hours ago
This seems overly complicated, not to mention that the lastawkis given an input file and ignores the pipe.
– RalfFriedl
2 hours ago
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♦
3 hours ago
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♦
3 hours ago
This seems overly complicated, not to mention that the last
awk is given an input file and ignores the pipe.– RalfFriedl
2 hours ago
This seems overly complicated, not to mention that the last
awk is given an input file and ignores the pipe.– RalfFriedl
2 hours ago
add a comment |
Ashish is a new contributor. Be nice, and check out our Code of Conduct.
Ashish is a new contributor. Be nice, and check out our Code of Conduct.
Ashish is a new contributor. Be nice, and check out our Code of Conduct.
Ashish is a new contributor. Be nice, and check out our Code of Conduct.
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
Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?
– terdon♦
9 hours ago