How to compare the content inside a txt file?
I have a txt file which named as output.txt:
output.txt
2128,4.4
2128,5.5
I wish to have a compare script that first line first number before comma is equal to second line first number, then it will return second line second number after comma.
output:
5.5
If the output.txt have different number for first line first number and second line first number, for example:
output.txt
2622,56
1011,65
Then expected output will be return a stringThe value is different
shell-script text-processing
New contributor
add a comment |
I have a txt file which named as output.txt:
output.txt
2128,4.4
2128,5.5
I wish to have a compare script that first line first number before comma is equal to second line first number, then it will return second line second number after comma.
output:
5.5
If the output.txt have different number for first line first number and second line first number, for example:
output.txt
2622,56
1011,65
Then expected output will be return a stringThe value is different
shell-script text-processing
New contributor
What have you tried?
– Nasir Riley
49 mins ago
sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer
– Shi Jie Tio
47 mins ago
sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?
– Shi Jie Tio
44 mins ago
add a comment |
I have a txt file which named as output.txt:
output.txt
2128,4.4
2128,5.5
I wish to have a compare script that first line first number before comma is equal to second line first number, then it will return second line second number after comma.
output:
5.5
If the output.txt have different number for first line first number and second line first number, for example:
output.txt
2622,56
1011,65
Then expected output will be return a stringThe value is different
shell-script text-processing
New contributor
I have a txt file which named as output.txt:
output.txt
2128,4.4
2128,5.5
I wish to have a compare script that first line first number before comma is equal to second line first number, then it will return second line second number after comma.
output:
5.5
If the output.txt have different number for first line first number and second line first number, for example:
output.txt
2622,56
1011,65
Then expected output will be return a stringThe value is different
shell-script text-processing
shell-script text-processing
New contributor
New contributor
New contributor
asked 1 hour ago
Shi Jie TioShi Jie Tio
1012
1012
New contributor
New contributor
What have you tried?
– Nasir Riley
49 mins ago
sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer
– Shi Jie Tio
47 mins ago
sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?
– Shi Jie Tio
44 mins ago
add a comment |
What have you tried?
– Nasir Riley
49 mins ago
sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer
– Shi Jie Tio
47 mins ago
sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?
– Shi Jie Tio
44 mins ago
What have you tried?
– Nasir Riley
49 mins ago
What have you tried?
– Nasir Riley
49 mins ago
sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer
– Shi Jie Tio
47 mins ago
sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer
– Shi Jie Tio
47 mins ago
sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?
– Shi Jie Tio
44 mins ago
sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?
– Shi Jie Tio
44 mins ago
add a comment |
1 Answer
1
active
oldest
votes
Confirmed in bash
:
#!/bin/bash
file=/path/to/output.txt
if [[ $(awk -F , 'NR==1 {print $1}' $file) -eq $(awk -F , 'NR==2 {print $1}' $file) ]]; then
awk -F , 'NR==2 {print $2}' $text
else
echo "The value is different"
fi
-The variable file
is assigned to the path of wherever output.txt is located.
-In the if
statement, awk
uses ,
as the delimiter and prints and compares the second values of the second column on each line.
-If the values are equal, then it prints the second column of the second line which is 5.5
-If the values are not equal, then it prints "The value is different".
I tested this with two files with the values that you specified. You can change the value of the file
variable in the script to work with others.
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
});
}
});
Shi Jie Tio 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%2f494344%2fhow-to-compare-the-content-inside-a-txt-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Confirmed in bash
:
#!/bin/bash
file=/path/to/output.txt
if [[ $(awk -F , 'NR==1 {print $1}' $file) -eq $(awk -F , 'NR==2 {print $1}' $file) ]]; then
awk -F , 'NR==2 {print $2}' $text
else
echo "The value is different"
fi
-The variable file
is assigned to the path of wherever output.txt is located.
-In the if
statement, awk
uses ,
as the delimiter and prints and compares the second values of the second column on each line.
-If the values are equal, then it prints the second column of the second line which is 5.5
-If the values are not equal, then it prints "The value is different".
I tested this with two files with the values that you specified. You can change the value of the file
variable in the script to work with others.
add a comment |
Confirmed in bash
:
#!/bin/bash
file=/path/to/output.txt
if [[ $(awk -F , 'NR==1 {print $1}' $file) -eq $(awk -F , 'NR==2 {print $1}' $file) ]]; then
awk -F , 'NR==2 {print $2}' $text
else
echo "The value is different"
fi
-The variable file
is assigned to the path of wherever output.txt is located.
-In the if
statement, awk
uses ,
as the delimiter and prints and compares the second values of the second column on each line.
-If the values are equal, then it prints the second column of the second line which is 5.5
-If the values are not equal, then it prints "The value is different".
I tested this with two files with the values that you specified. You can change the value of the file
variable in the script to work with others.
add a comment |
Confirmed in bash
:
#!/bin/bash
file=/path/to/output.txt
if [[ $(awk -F , 'NR==1 {print $1}' $file) -eq $(awk -F , 'NR==2 {print $1}' $file) ]]; then
awk -F , 'NR==2 {print $2}' $text
else
echo "The value is different"
fi
-The variable file
is assigned to the path of wherever output.txt is located.
-In the if
statement, awk
uses ,
as the delimiter and prints and compares the second values of the second column on each line.
-If the values are equal, then it prints the second column of the second line which is 5.5
-If the values are not equal, then it prints "The value is different".
I tested this with two files with the values that you specified. You can change the value of the file
variable in the script to work with others.
Confirmed in bash
:
#!/bin/bash
file=/path/to/output.txt
if [[ $(awk -F , 'NR==1 {print $1}' $file) -eq $(awk -F , 'NR==2 {print $1}' $file) ]]; then
awk -F , 'NR==2 {print $2}' $text
else
echo "The value is different"
fi
-The variable file
is assigned to the path of wherever output.txt is located.
-In the if
statement, awk
uses ,
as the delimiter and prints and compares the second values of the second column on each line.
-If the values are equal, then it prints the second column of the second line which is 5.5
-If the values are not equal, then it prints "The value is different".
I tested this with two files with the values that you specified. You can change the value of the file
variable in the script to work with others.
answered 15 mins ago
Nasir RileyNasir Riley
2,406239
2,406239
add a comment |
add a comment |
Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.
Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.
Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.
Shi Jie Tio 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%2f494344%2fhow-to-compare-the-content-inside-a-txt-file%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
What have you tried?
– Nasir Riley
49 mins ago
sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer
– Shi Jie Tio
47 mins ago
sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?
– Shi Jie Tio
44 mins ago