Compare data in 5th column with 6th column and print the least difference element

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a file with below data:



APPLE,FRUIT,1KG,RED,1660.00|1680.00|1700.00|1720.00|1740.00|1760.00|1800.00|1900.00|1920.00|1520.00|1320.00|1340.00|1360.00|1380.00|1400.00|1420.00|1440.00|1460.00|1480.00|1940.00|940.00|1880.00|1820.00|1840.00|1860.00|1780.00|1500.00|1020.00|1040.00|1060.00|1080.00|1100.00|1120.00|1140.00|1160.00|1180.00|1200.00|1220.00|1240.00|1260.00|1280.00|1300.00|960.00|980.00|1000.00|1540.00|1560.00|1580.00|1600.00|1620.00|1640.00,1429.100000
ORANGE,FRUIT,2KG,GREEN,1660.00|1680.00|1700.00|1720.00|1740.00|1760.00|1900.00|1920.00|1500.00|1320.00|1340.00|1360.00|1380.00|1400.00|1420.00|1440.00|1460.00|1940.00|1800.00|940.00|1880.00|1820.00|1840.00|1860.00|1780.00|1480.00|1020.00|1040.00|1060.00|1080.00|1100.00|1120.00|1140.00|1160.00|1180.00|1200.00|1220.00|1240.00|1260.00|1280.00|1300.00|960.00|980.00|1000.00|1520.00|1540.00|1560.00|1580.00|1600.00|1620.00|1640.00,1432.100000


I want to check each value in column 5(delimiter to get column 5 is ","). Compare the same with column 6 and get the least difference element into the new file.



Expected output is:



APPLE,FRUIT,1KG,RED,1420.00
ORANGE,FRUIT,2KG,GREEN,1440.00


My file has more than 200 rows, looking for a one liner command to do the same.










share|improve this question



















  • 4





    Your name is 'programmer'... well what have you 'programmed' on your own so far? Where are you stuck?

    – Panki
    Mar 14 at 9:04






  • 1





    I cooked up a oneline solution, but it's in python and the tags state bash.

    – Panki
    Mar 14 at 9:16






  • 1





    @Panki please don't post answers as comments.

    – terdon
    Mar 14 at 9:34






  • 1





    @Panki if it's a one-liner, then sure, you may as well.

    – terdon
    Mar 14 at 9:38






  • 1





    Yes it needs to process the file line by line. I don't really know what other solution there would be.

    – Panki
    Mar 14 at 9:45

















1















I have a file with below data:



APPLE,FRUIT,1KG,RED,1660.00|1680.00|1700.00|1720.00|1740.00|1760.00|1800.00|1900.00|1920.00|1520.00|1320.00|1340.00|1360.00|1380.00|1400.00|1420.00|1440.00|1460.00|1480.00|1940.00|940.00|1880.00|1820.00|1840.00|1860.00|1780.00|1500.00|1020.00|1040.00|1060.00|1080.00|1100.00|1120.00|1140.00|1160.00|1180.00|1200.00|1220.00|1240.00|1260.00|1280.00|1300.00|960.00|980.00|1000.00|1540.00|1560.00|1580.00|1600.00|1620.00|1640.00,1429.100000
ORANGE,FRUIT,2KG,GREEN,1660.00|1680.00|1700.00|1720.00|1740.00|1760.00|1900.00|1920.00|1500.00|1320.00|1340.00|1360.00|1380.00|1400.00|1420.00|1440.00|1460.00|1940.00|1800.00|940.00|1880.00|1820.00|1840.00|1860.00|1780.00|1480.00|1020.00|1040.00|1060.00|1080.00|1100.00|1120.00|1140.00|1160.00|1180.00|1200.00|1220.00|1240.00|1260.00|1280.00|1300.00|960.00|980.00|1000.00|1520.00|1540.00|1560.00|1580.00|1600.00|1620.00|1640.00,1432.100000


I want to check each value in column 5(delimiter to get column 5 is ","). Compare the same with column 6 and get the least difference element into the new file.



Expected output is:



APPLE,FRUIT,1KG,RED,1420.00
ORANGE,FRUIT,2KG,GREEN,1440.00


My file has more than 200 rows, looking for a one liner command to do the same.










share|improve this question



















  • 4





    Your name is 'programmer'... well what have you 'programmed' on your own so far? Where are you stuck?

    – Panki
    Mar 14 at 9:04






  • 1





    I cooked up a oneline solution, but it's in python and the tags state bash.

    – Panki
    Mar 14 at 9:16






  • 1





    @Panki please don't post answers as comments.

    – terdon
    Mar 14 at 9:34






  • 1





    @Panki if it's a one-liner, then sure, you may as well.

    – terdon
    Mar 14 at 9:38






  • 1





    Yes it needs to process the file line by line. I don't really know what other solution there would be.

    – Panki
    Mar 14 at 9:45













1












1








1








I have a file with below data:



APPLE,FRUIT,1KG,RED,1660.00|1680.00|1700.00|1720.00|1740.00|1760.00|1800.00|1900.00|1920.00|1520.00|1320.00|1340.00|1360.00|1380.00|1400.00|1420.00|1440.00|1460.00|1480.00|1940.00|940.00|1880.00|1820.00|1840.00|1860.00|1780.00|1500.00|1020.00|1040.00|1060.00|1080.00|1100.00|1120.00|1140.00|1160.00|1180.00|1200.00|1220.00|1240.00|1260.00|1280.00|1300.00|960.00|980.00|1000.00|1540.00|1560.00|1580.00|1600.00|1620.00|1640.00,1429.100000
ORANGE,FRUIT,2KG,GREEN,1660.00|1680.00|1700.00|1720.00|1740.00|1760.00|1900.00|1920.00|1500.00|1320.00|1340.00|1360.00|1380.00|1400.00|1420.00|1440.00|1460.00|1940.00|1800.00|940.00|1880.00|1820.00|1840.00|1860.00|1780.00|1480.00|1020.00|1040.00|1060.00|1080.00|1100.00|1120.00|1140.00|1160.00|1180.00|1200.00|1220.00|1240.00|1260.00|1280.00|1300.00|960.00|980.00|1000.00|1520.00|1540.00|1560.00|1580.00|1600.00|1620.00|1640.00,1432.100000


I want to check each value in column 5(delimiter to get column 5 is ","). Compare the same with column 6 and get the least difference element into the new file.



Expected output is:



APPLE,FRUIT,1KG,RED,1420.00
ORANGE,FRUIT,2KG,GREEN,1440.00


My file has more than 200 rows, looking for a one liner command to do the same.










share|improve this question
















I have a file with below data:



APPLE,FRUIT,1KG,RED,1660.00|1680.00|1700.00|1720.00|1740.00|1760.00|1800.00|1900.00|1920.00|1520.00|1320.00|1340.00|1360.00|1380.00|1400.00|1420.00|1440.00|1460.00|1480.00|1940.00|940.00|1880.00|1820.00|1840.00|1860.00|1780.00|1500.00|1020.00|1040.00|1060.00|1080.00|1100.00|1120.00|1140.00|1160.00|1180.00|1200.00|1220.00|1240.00|1260.00|1280.00|1300.00|960.00|980.00|1000.00|1540.00|1560.00|1580.00|1600.00|1620.00|1640.00,1429.100000
ORANGE,FRUIT,2KG,GREEN,1660.00|1680.00|1700.00|1720.00|1740.00|1760.00|1900.00|1920.00|1500.00|1320.00|1340.00|1360.00|1380.00|1400.00|1420.00|1440.00|1460.00|1940.00|1800.00|940.00|1880.00|1820.00|1840.00|1860.00|1780.00|1480.00|1020.00|1040.00|1060.00|1080.00|1100.00|1120.00|1140.00|1160.00|1180.00|1200.00|1220.00|1240.00|1260.00|1280.00|1300.00|960.00|980.00|1000.00|1520.00|1540.00|1560.00|1580.00|1600.00|1620.00|1640.00,1432.100000


I want to check each value in column 5(delimiter to get column 5 is ","). Compare the same with column 6 and get the least difference element into the new file.



Expected output is:



APPLE,FRUIT,1KG,RED,1420.00
ORANGE,FRUIT,2KG,GREEN,1440.00


My file has more than 200 rows, looking for a one liner command to do the same.







text-processing awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 14 at 9:37







Programmer

















asked Mar 14 at 8:49









ProgrammerProgrammer

459




459







  • 4





    Your name is 'programmer'... well what have you 'programmed' on your own so far? Where are you stuck?

    – Panki
    Mar 14 at 9:04






  • 1





    I cooked up a oneline solution, but it's in python and the tags state bash.

    – Panki
    Mar 14 at 9:16






  • 1





    @Panki please don't post answers as comments.

    – terdon
    Mar 14 at 9:34






  • 1





    @Panki if it's a one-liner, then sure, you may as well.

    – terdon
    Mar 14 at 9:38






  • 1





    Yes it needs to process the file line by line. I don't really know what other solution there would be.

    – Panki
    Mar 14 at 9:45












  • 4





    Your name is 'programmer'... well what have you 'programmed' on your own so far? Where are you stuck?

    – Panki
    Mar 14 at 9:04






  • 1





    I cooked up a oneline solution, but it's in python and the tags state bash.

    – Panki
    Mar 14 at 9:16






  • 1





    @Panki please don't post answers as comments.

    – terdon
    Mar 14 at 9:34






  • 1





    @Panki if it's a one-liner, then sure, you may as well.

    – terdon
    Mar 14 at 9:38






  • 1





    Yes it needs to process the file line by line. I don't really know what other solution there would be.

    – Panki
    Mar 14 at 9:45







4




4





Your name is 'programmer'... well what have you 'programmed' on your own so far? Where are you stuck?

– Panki
Mar 14 at 9:04





Your name is 'programmer'... well what have you 'programmed' on your own so far? Where are you stuck?

– Panki
Mar 14 at 9:04




1




1





I cooked up a oneline solution, but it's in python and the tags state bash.

– Panki
Mar 14 at 9:16





I cooked up a oneline solution, but it's in python and the tags state bash.

– Panki
Mar 14 at 9:16




1




1





@Panki please don't post answers as comments.

– terdon
Mar 14 at 9:34





@Panki please don't post answers as comments.

– terdon
Mar 14 at 9:34




1




1





@Panki if it's a one-liner, then sure, you may as well.

– terdon
Mar 14 at 9:38





@Panki if it's a one-liner, then sure, you may as well.

– terdon
Mar 14 at 9:38




1




1





Yes it needs to process the file line by line. I don't really know what other solution there would be.

– Panki
Mar 14 at 9:45





Yes it needs to process the file line by line. I don't really know what other solution there would be.

– Panki
Mar 14 at 9:45










2 Answers
2






active

oldest

votes


















1














Here is my Python solution.



with open('/path/to/file', 'r') as f:
for line in f:
x=line.split(',')
y=x[4].split('|')
print(','.join(x[:4]) + ',' + str(min(y, key=lambda z: abs(float(z)-float(x[5])))))


If you have something like Pyed Piper, also known as pyp, you can run this straight from the terminal prompt: https://code.google.com/archive/p/pyp/






share|improve this answer

























  • Thanks for the share. I am able to get the desired result with this.

    – Programmer
    Mar 14 at 10:08


















2














As an AWK solution is requested, I will give one.



awk -F, 'NF>4
n=split($5,a,"' input > output





share|improve this answer


















  • 1





    +1, clever use of ^2 to get around the absence of an abs function in awk

    – iruvar
    Mar 15 at 19:25











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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506237%2fcompare-data-in-5th-column-with-6th-column-and-print-the-least-difference-elemen%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Here is my Python solution.



with open('/path/to/file', 'r') as f:
for line in f:
x=line.split(',')
y=x[4].split('|')
print(','.join(x[:4]) + ',' + str(min(y, key=lambda z: abs(float(z)-float(x[5])))))


If you have something like Pyed Piper, also known as pyp, you can run this straight from the terminal prompt: https://code.google.com/archive/p/pyp/






share|improve this answer

























  • Thanks for the share. I am able to get the desired result with this.

    – Programmer
    Mar 14 at 10:08















1














Here is my Python solution.



with open('/path/to/file', 'r') as f:
for line in f:
x=line.split(',')
y=x[4].split('|')
print(','.join(x[:4]) + ',' + str(min(y, key=lambda z: abs(float(z)-float(x[5])))))


If you have something like Pyed Piper, also known as pyp, you can run this straight from the terminal prompt: https://code.google.com/archive/p/pyp/






share|improve this answer

























  • Thanks for the share. I am able to get the desired result with this.

    – Programmer
    Mar 14 at 10:08













1












1








1







Here is my Python solution.



with open('/path/to/file', 'r') as f:
for line in f:
x=line.split(',')
y=x[4].split('|')
print(','.join(x[:4]) + ',' + str(min(y, key=lambda z: abs(float(z)-float(x[5])))))


If you have something like Pyed Piper, also known as pyp, you can run this straight from the terminal prompt: https://code.google.com/archive/p/pyp/






share|improve this answer















Here is my Python solution.



with open('/path/to/file', 'r') as f:
for line in f:
x=line.split(',')
y=x[4].split('|')
print(','.join(x[:4]) + ',' + str(min(y, key=lambda z: abs(float(z)-float(x[5])))))


If you have something like Pyed Piper, also known as pyp, you can run this straight from the terminal prompt: https://code.google.com/archive/p/pyp/







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 14 at 10:03









terdon

134k33270450




134k33270450










answered Mar 14 at 9:51









PankiPanki

868512




868512












  • Thanks for the share. I am able to get the desired result with this.

    – Programmer
    Mar 14 at 10:08

















  • Thanks for the share. I am able to get the desired result with this.

    – Programmer
    Mar 14 at 10:08
















Thanks for the share. I am able to get the desired result with this.

– Programmer
Mar 14 at 10:08





Thanks for the share. I am able to get the desired result with this.

– Programmer
Mar 14 at 10:08













2














As an AWK solution is requested, I will give one.



awk -F, 'NF>4
n=split($5,a,"' input > output





share|improve this answer


















  • 1





    +1, clever use of ^2 to get around the absence of an abs function in awk

    – iruvar
    Mar 15 at 19:25















2














As an AWK solution is requested, I will give one.



awk -F, 'NF>4
n=split($5,a,"' input > output





share|improve this answer


















  • 1





    +1, clever use of ^2 to get around the absence of an abs function in awk

    – iruvar
    Mar 15 at 19:25













2












2








2







As an AWK solution is requested, I will give one.



awk -F, 'NF>4
n=split($5,a,"' input > output





share|improve this answer













As an AWK solution is requested, I will give one.



awk -F, 'NF>4
n=split($5,a,"' input > output






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 15 at 18:19









Weijun ZhouWeijun Zhou

1,681427




1,681427







  • 1





    +1, clever use of ^2 to get around the absence of an abs function in awk

    – iruvar
    Mar 15 at 19:25












  • 1





    +1, clever use of ^2 to get around the absence of an abs function in awk

    – iruvar
    Mar 15 at 19:25







1




1





+1, clever use of ^2 to get around the absence of an abs function in awk

– iruvar
Mar 15 at 19:25





+1, clever use of ^2 to get around the absence of an abs function in awk

– iruvar
Mar 15 at 19:25

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506237%2fcompare-data-in-5th-column-with-6th-column-and-print-the-least-difference-elemen%23new-answer', 'question_page');

);

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






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay