Sort File with word2019 Community Moderator Electionsort a file based on length of the column/rowGrouped sorting of continuous paragraphs (separated by blank line)?Sort -u without sorting but with better uniqueness?sort behaves weirdly with scientific notationSort text by specific location on each lineSort unix alphabetically then numerically, not working as I intendedremove all duplicates from a text file without sortSort text files with multiple lines as a rowSort by Regex when reviewing fileHow to print all the lines that's first word is the first word of a file?
Am I not good enough for you?
GPLv2 - licensing for commercial use
Why was Goose renamed from Chewie for the Captain Marvel film?
Can someone explain what is being said here in color publishing in the American Mathematical Monthly?
How are showroom/display vehicles prepared?
How do you like my writing?
Could you please stop shuffling the deck and play already?
Should I tell my boss the work he did was worthless
Examples of a statistic that is not independent of sample's distribution?
What to do when during a meeting client people start to fight (even physically) with each others?
How do I deal with a powergamer in a game full of beginners in a school club?
Is it true that real estate prices mainly go up?
Intuition behind counterexample of Euler's sum of powers conjecture
Tricky AM-GM inequality
Try Catch Block Affecting a Variable in an Enclosing Scope
Virginia employer terminated employee and wants signing bonus returned
Finding algorithms of QGIS commands?
How to pass a string to a command that expects a file?
infinitive telling the purpose
How to create a hard link to an inode (ext4)?
Accepted offer letter, position changed
Force user to remove USB token
Best approach to update all entries in a list that is paginated?
Bash script should only kill those instances of another script's that it has launched
Sort File with word
2019 Community Moderator Electionsort a file based on length of the column/rowGrouped sorting of continuous paragraphs (separated by blank line)?Sort -u without sorting but with better uniqueness?sort behaves weirdly with scientific notationSort text by specific location on each lineSort unix alphabetically then numerically, not working as I intendedremove all duplicates from a text file without sortSort text files with multiple lines as a rowSort by Regex when reviewing fileHow to print all the lines that's first word is the first word of a file?
I have a text file that looks something like this
Name1 OpenFin
Name2 Chrome
Name3 OpenFin
Name4 Chrome
Name5 OpenFin
Name6 OpenFin
I want to sort this is a way where all lines with OpenFin
appears first followed by all lines with Chrome
This is what I have so far:
grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt
linux text-processing sort
add a comment |
I have a text file that looks something like this
Name1 OpenFin
Name2 Chrome
Name3 OpenFin
Name4 Chrome
Name5 OpenFin
Name6 OpenFin
I want to sort this is a way where all lines with OpenFin
appears first followed by all lines with Chrome
This is what I have so far:
grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt
linux text-processing sort
3
sort -k2r LoginExternal.txt
– steeldriver
2 hours ago
Genius! Can you explain how this works?
– Ibrahim A
2 hours ago
I also need the names to stay in alphabetical order
– Ibrahim A
2 hours ago
The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNUsort
then you can trysort --stable -k2r LoginExternal.txt
– steeldriver
2 hours ago
add a comment |
I have a text file that looks something like this
Name1 OpenFin
Name2 Chrome
Name3 OpenFin
Name4 Chrome
Name5 OpenFin
Name6 OpenFin
I want to sort this is a way where all lines with OpenFin
appears first followed by all lines with Chrome
This is what I have so far:
grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt
linux text-processing sort
I have a text file that looks something like this
Name1 OpenFin
Name2 Chrome
Name3 OpenFin
Name4 Chrome
Name5 OpenFin
Name6 OpenFin
I want to sort this is a way where all lines with OpenFin
appears first followed by all lines with Chrome
This is what I have so far:
grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt
linux text-processing sort
linux text-processing sort
edited 1 hour ago
Jeff Schaller
43.3k1159139
43.3k1159139
asked 2 hours ago
Ibrahim AIbrahim A
379
379
3
sort -k2r LoginExternal.txt
– steeldriver
2 hours ago
Genius! Can you explain how this works?
– Ibrahim A
2 hours ago
I also need the names to stay in alphabetical order
– Ibrahim A
2 hours ago
The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNUsort
then you can trysort --stable -k2r LoginExternal.txt
– steeldriver
2 hours ago
add a comment |
3
sort -k2r LoginExternal.txt
– steeldriver
2 hours ago
Genius! Can you explain how this works?
– Ibrahim A
2 hours ago
I also need the names to stay in alphabetical order
– Ibrahim A
2 hours ago
The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNUsort
then you can trysort --stable -k2r LoginExternal.txt
– steeldriver
2 hours ago
3
3
sort -k2r LoginExternal.txt
– steeldriver
2 hours ago
sort -k2r LoginExternal.txt
– steeldriver
2 hours ago
Genius! Can you explain how this works?
– Ibrahim A
2 hours ago
Genius! Can you explain how this works?
– Ibrahim A
2 hours ago
I also need the names to stay in alphabetical order
– Ibrahim A
2 hours ago
I also need the names to stay in alphabetical order
– Ibrahim A
2 hours ago
The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNU
sort
then you can try sort --stable -k2r LoginExternal.txt
– steeldriver
2 hours ago
The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNU
sort
then you can try sort --stable -k2r LoginExternal.txt
– steeldriver
2 hours ago
add a comment |
3 Answers
3
active
oldest
votes
A direct translation of your requirement:
all lines with
OpenFin
appears first, followed by all lines withChrome
... without having to know which sorts first would be:
grep -F OpenFin LoginExcInternal.txt; grep -F Chrome LoginExcInternal.txt; > test.txt
This also keeps the lines in the order in which they appeared in the original file (without going through hoops to keep sort
from sorting on the whole line).
add a comment |
Your command
grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt
would only produce the lines from the original data that contains the string Chrome
anywhere on the line. This is because the result of the first grep
in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).
grep
will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first grep
arrives.
sort -k2r LoginExcInternal.txt >test.txt
You want the lines with Chrome
in their 2nd column to come after the lines containing OpenFin
in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.
The -k2r
flag to sort
tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the r
). If two lines have the same value in the second column, then the whole line will be used as the sorting key.
Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.
If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:
awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt
This applies the same awk
code twice on the original data, with different values in the variable value
. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the awk
script does the OpenFin
values, and the second adds the Chrome
lines to this.
This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be Chrome
or OpenFin
. Also, since the awk
code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.
add a comment |
This was answered by the user "emilrn" then removed for some reason. However, their response worked perfectly.
I was told to run both of these commands:
cat LoginExcInternal.txt | grep OpenFin >> test.txt
cat LoginExcInternal.txt | grep Chrome >> test.txt
Thank you emilrn!
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%2f505891%2fsort-file-with-word%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
A direct translation of your requirement:
all lines with
OpenFin
appears first, followed by all lines withChrome
... without having to know which sorts first would be:
grep -F OpenFin LoginExcInternal.txt; grep -F Chrome LoginExcInternal.txt; > test.txt
This also keeps the lines in the order in which they appeared in the original file (without going through hoops to keep sort
from sorting on the whole line).
add a comment |
A direct translation of your requirement:
all lines with
OpenFin
appears first, followed by all lines withChrome
... without having to know which sorts first would be:
grep -F OpenFin LoginExcInternal.txt; grep -F Chrome LoginExcInternal.txt; > test.txt
This also keeps the lines in the order in which they appeared in the original file (without going through hoops to keep sort
from sorting on the whole line).
add a comment |
A direct translation of your requirement:
all lines with
OpenFin
appears first, followed by all lines withChrome
... without having to know which sorts first would be:
grep -F OpenFin LoginExcInternal.txt; grep -F Chrome LoginExcInternal.txt; > test.txt
This also keeps the lines in the order in which they appeared in the original file (without going through hoops to keep sort
from sorting on the whole line).
A direct translation of your requirement:
all lines with
OpenFin
appears first, followed by all lines withChrome
... without having to know which sorts first would be:
grep -F OpenFin LoginExcInternal.txt; grep -F Chrome LoginExcInternal.txt; > test.txt
This also keeps the lines in the order in which they appeared in the original file (without going through hoops to keep sort
from sorting on the whole line).
answered 1 hour ago
Jeff SchallerJeff Schaller
43.3k1159139
43.3k1159139
add a comment |
add a comment |
Your command
grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt
would only produce the lines from the original data that contains the string Chrome
anywhere on the line. This is because the result of the first grep
in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).
grep
will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first grep
arrives.
sort -k2r LoginExcInternal.txt >test.txt
You want the lines with Chrome
in their 2nd column to come after the lines containing OpenFin
in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.
The -k2r
flag to sort
tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the r
). If two lines have the same value in the second column, then the whole line will be used as the sorting key.
Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.
If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:
awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt
This applies the same awk
code twice on the original data, with different values in the variable value
. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the awk
script does the OpenFin
values, and the second adds the Chrome
lines to this.
This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be Chrome
or OpenFin
. Also, since the awk
code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.
add a comment |
Your command
grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt
would only produce the lines from the original data that contains the string Chrome
anywhere on the line. This is because the result of the first grep
in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).
grep
will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first grep
arrives.
sort -k2r LoginExcInternal.txt >test.txt
You want the lines with Chrome
in their 2nd column to come after the lines containing OpenFin
in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.
The -k2r
flag to sort
tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the r
). If two lines have the same value in the second column, then the whole line will be used as the sorting key.
Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.
If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:
awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt
This applies the same awk
code twice on the original data, with different values in the variable value
. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the awk
script does the OpenFin
values, and the second adds the Chrome
lines to this.
This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be Chrome
or OpenFin
. Also, since the awk
code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.
add a comment |
Your command
grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt
would only produce the lines from the original data that contains the string Chrome
anywhere on the line. This is because the result of the first grep
in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).
grep
will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first grep
arrives.
sort -k2r LoginExcInternal.txt >test.txt
You want the lines with Chrome
in their 2nd column to come after the lines containing OpenFin
in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.
The -k2r
flag to sort
tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the r
). If two lines have the same value in the second column, then the whole line will be used as the sorting key.
Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.
If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:
awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt
This applies the same awk
code twice on the original data, with different values in the variable value
. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the awk
script does the OpenFin
values, and the second adds the Chrome
lines to this.
This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be Chrome
or OpenFin
. Also, since the awk
code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.
Your command
grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt
would only produce the lines from the original data that contains the string Chrome
anywhere on the line. This is because the result of the first grep
in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).
grep
will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first grep
arrives.
sort -k2r LoginExcInternal.txt >test.txt
You want the lines with Chrome
in their 2nd column to come after the lines containing OpenFin
in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.
The -k2r
flag to sort
tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the r
). If two lines have the same value in the second column, then the whole line will be used as the sorting key.
Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.
If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:
awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt
This applies the same awk
code twice on the original data, with different values in the variable value
. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the awk
script does the OpenFin
values, and the second adds the Chrome
lines to this.
This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be Chrome
or OpenFin
. Also, since the awk
code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.
edited 52 mins ago
answered 1 hour ago
KusalanandaKusalananda
135k17255421
135k17255421
add a comment |
add a comment |
This was answered by the user "emilrn" then removed for some reason. However, their response worked perfectly.
I was told to run both of these commands:
cat LoginExcInternal.txt | grep OpenFin >> test.txt
cat LoginExcInternal.txt | grep Chrome >> test.txt
Thank you emilrn!
add a comment |
This was answered by the user "emilrn" then removed for some reason. However, their response worked perfectly.
I was told to run both of these commands:
cat LoginExcInternal.txt | grep OpenFin >> test.txt
cat LoginExcInternal.txt | grep Chrome >> test.txt
Thank you emilrn!
add a comment |
This was answered by the user "emilrn" then removed for some reason. However, their response worked perfectly.
I was told to run both of these commands:
cat LoginExcInternal.txt | grep OpenFin >> test.txt
cat LoginExcInternal.txt | grep Chrome >> test.txt
Thank you emilrn!
This was answered by the user "emilrn" then removed for some reason. However, their response worked perfectly.
I was told to run both of these commands:
cat LoginExcInternal.txt | grep OpenFin >> test.txt
cat LoginExcInternal.txt | grep Chrome >> test.txt
Thank you emilrn!
answered 2 hours ago
Ibrahim AIbrahim A
379
379
add a comment |
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%2f505891%2fsort-file-with-word%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
-linux, sort, text-processing
3
sort -k2r LoginExternal.txt
– steeldriver
2 hours ago
Genius! Can you explain how this works?
– Ibrahim A
2 hours ago
I also need the names to stay in alphabetical order
– Ibrahim A
2 hours ago
The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNU
sort
then you can trysort --stable -k2r LoginExternal.txt
– steeldriver
2 hours ago