extract characters between two commas? The 2019 Stack Overflow Developer Survey Results Are InText processing - join every two lines with commasRemove entire row in a file if first column is repeatedMerging two files, one column at a timeJoin two textfiles on 1st column keeping order and unpairable lines from 1st fileHow to print lines if two fields have identical values?Delete lines that matches a string before commahow to remove comma and strings after a comma in a file?Fastest way to sum Nth column in text fileHow to extract the first row for each entry in the first column?Compare two files and get unmatched rows from the second file based on first and second column
Families of ordered set partitions with disjoint blocks
Why is it "Tumoren" and not "Tumore"?
Extreme, unacceptable situation and I can't attend work tomorrow morning
Are there any other methods to apply to solving simultaneous equations?
How to create dashed lines/arrows in Illustrator CC
How can I fix this gap between bookcases I made?
Is this food a bread or a loaf?
What do hard-Brexiteers want with respect to the Irish border?
Idomatic way to prevent slicing?
What is this 4-propeller plane?
Why don't Unix/Linux systems traverse through directories until they find the required version of a linked library?
The difference between dialogue marks
Is domain driven design an anti-SQL pattern?
Can we apply L'Hospital's rule?
Why isn't airport relocation done gradually?
Could JWST stay at L2 "forever"?
If Wish Duplicates Simulacrum, Are Existing Duplicates Destroyed?
Poison Arrows Piercing damage reduced to 0, do you still get poisoned?
Inflated grade on resume at previous job, might former employer tell new employer?
Time travel alters history but people keep saying nothing's changed
Can I write a for loop that iterates over both collections and arrays?
A poker game description that does not feel gimmicky
Deadlock Graph and Interpretation, solution to avoid
What does "sndry explns" mean in one of the Hitchhiker's guide books?
extract characters between two commas?
The 2019 Stack Overflow Developer Survey Results Are InText processing - join every two lines with commasRemove entire row in a file if first column is repeatedMerging two files, one column at a timeJoin two textfiles on 1st column keeping order and unpairable lines from 1st fileHow to print lines if two fields have identical values?Delete lines that matches a string before commahow to remove comma and strings after a comma in a file?Fastest way to sum Nth column in text fileHow to extract the first row for each entry in the first column?Compare two files and get unmatched rows from the second file based on first and second column
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a file with ~ 3 million rows, here is the first few lines of my file:
head out.txt
NA
NA
NA
NA
NA
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752
gene85752
For those rows that are separated by ",", I want to keep everything after the first comma and before the second comma.
This is my desired output:
outgood.txt
NA
NA
NA
NA
NA
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85752
gene85752
text-processing awk
add a comment |
I have a file with ~ 3 million rows, here is the first few lines of my file:
head out.txt
NA
NA
NA
NA
NA
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752
gene85752
For those rows that are separated by ",", I want to keep everything after the first comma and before the second comma.
This is my desired output:
outgood.txt
NA
NA
NA
NA
NA
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85752
gene85752
text-processing awk
add a comment |
I have a file with ~ 3 million rows, here is the first few lines of my file:
head out.txt
NA
NA
NA
NA
NA
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752
gene85752
For those rows that are separated by ",", I want to keep everything after the first comma and before the second comma.
This is my desired output:
outgood.txt
NA
NA
NA
NA
NA
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85752
gene85752
text-processing awk
I have a file with ~ 3 million rows, here is the first few lines of my file:
head out.txt
NA
NA
NA
NA
NA
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753,gene85754
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752,gene85753
gene85752
gene85752
For those rows that are separated by ",", I want to keep everything after the first comma and before the second comma.
This is my desired output:
outgood.txt
NA
NA
NA
NA
NA
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85752
gene85752
text-processing awk
text-processing awk
asked yesterday
Anna1364Anna1364
456213
456213
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Since cut prints non-delimited lines by default the following works
cut -f2 -d, file
1
It's nice when someone remember the little quirks of standard tools.
– Kusalananda♦
yesterday
add a comment |
awk -F, 'NF > 1 $1 = $2 print $1 ' file
This uses awk to parse the file as lines consisting of comma-delimited fields.
The code detects when there is more than a single field on a line, and when there is, the first field is replaced by the second field. The first field, either unmodified or modified by the conditional code, is then printed.
With a big file, this would probably be faster:awk -F, 'print(NF>1 ? $2 : $1)'-- since you won't have to rewrite $0
– glenn jackman
yesterday
@glennjackman Well, thecutsolution would be even faster in any case.
– Kusalananda♦
yesterday
add a comment |
awk -F, 'NF == 1 print $1
NF > 1 print $2' filename
This will print just the first string if there is no comma, second string if there is one or more comma.
add a comment |
You can do this with Perl as follows.
Command-line:
$ perl -F, -pale '$_ = $F[1] // $_' out.txt
Explanation:
-pwill read records line-by-line AND autoprint before going in to read the next or eof.-lmakesIRS = ORS = "n"-F,makesFSa comma.-asplits each record$_on the field separator, in our case a comma, and goes ahead and stores the fields so generated in the array@F, which is zero-indexed.-eimplies, what follows it is thePerlcode, which shall be gets applied to each record.$_ = $F[1] // $_expression reads as follows: if the 2nd field$F[1]isn't defined, use the current record$_. And then the result of this expression is assigned to the current record$_.- owing to the
-pswitch ofperlbeing in use, before the new record is read in, the current record is taken tostdout.
Result:
NA
NA
NA
NA
NA
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85752
gene85752
You may also do it with the GNU version of the sed editor as shown below:
$ sed -ne '
s/,/n/
s/.*n//
s/,/n/
P
' out.txt
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%2f511284%2fextract-characters-between-two-commas%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since cut prints non-delimited lines by default the following works
cut -f2 -d, file
1
It's nice when someone remember the little quirks of standard tools.
– Kusalananda♦
yesterday
add a comment |
Since cut prints non-delimited lines by default the following works
cut -f2 -d, file
1
It's nice when someone remember the little quirks of standard tools.
– Kusalananda♦
yesterday
add a comment |
Since cut prints non-delimited lines by default the following works
cut -f2 -d, file
Since cut prints non-delimited lines by default the following works
cut -f2 -d, file
answered yesterday
iruvariruvar
12.4k63063
12.4k63063
1
It's nice when someone remember the little quirks of standard tools.
– Kusalananda♦
yesterday
add a comment |
1
It's nice when someone remember the little quirks of standard tools.
– Kusalananda♦
yesterday
1
1
It's nice when someone remember the little quirks of standard tools.
– Kusalananda♦
yesterday
It's nice when someone remember the little quirks of standard tools.
– Kusalananda♦
yesterday
add a comment |
awk -F, 'NF > 1 $1 = $2 print $1 ' file
This uses awk to parse the file as lines consisting of comma-delimited fields.
The code detects when there is more than a single field on a line, and when there is, the first field is replaced by the second field. The first field, either unmodified or modified by the conditional code, is then printed.
With a big file, this would probably be faster:awk -F, 'print(NF>1 ? $2 : $1)'-- since you won't have to rewrite $0
– glenn jackman
yesterday
@glennjackman Well, thecutsolution would be even faster in any case.
– Kusalananda♦
yesterday
add a comment |
awk -F, 'NF > 1 $1 = $2 print $1 ' file
This uses awk to parse the file as lines consisting of comma-delimited fields.
The code detects when there is more than a single field on a line, and when there is, the first field is replaced by the second field. The first field, either unmodified or modified by the conditional code, is then printed.
With a big file, this would probably be faster:awk -F, 'print(NF>1 ? $2 : $1)'-- since you won't have to rewrite $0
– glenn jackman
yesterday
@glennjackman Well, thecutsolution would be even faster in any case.
– Kusalananda♦
yesterday
add a comment |
awk -F, 'NF > 1 $1 = $2 print $1 ' file
This uses awk to parse the file as lines consisting of comma-delimited fields.
The code detects when there is more than a single field on a line, and when there is, the first field is replaced by the second field. The first field, either unmodified or modified by the conditional code, is then printed.
awk -F, 'NF > 1 $1 = $2 print $1 ' file
This uses awk to parse the file as lines consisting of comma-delimited fields.
The code detects when there is more than a single field on a line, and when there is, the first field is replaced by the second field. The first field, either unmodified or modified by the conditional code, is then printed.
answered yesterday
Kusalananda♦Kusalananda
140k17261436
140k17261436
With a big file, this would probably be faster:awk -F, 'print(NF>1 ? $2 : $1)'-- since you won't have to rewrite $0
– glenn jackman
yesterday
@glennjackman Well, thecutsolution would be even faster in any case.
– Kusalananda♦
yesterday
add a comment |
With a big file, this would probably be faster:awk -F, 'print(NF>1 ? $2 : $1)'-- since you won't have to rewrite $0
– glenn jackman
yesterday
@glennjackman Well, thecutsolution would be even faster in any case.
– Kusalananda♦
yesterday
With a big file, this would probably be faster:
awk -F, 'print(NF>1 ? $2 : $1)' -- since you won't have to rewrite $0– glenn jackman
yesterday
With a big file, this would probably be faster:
awk -F, 'print(NF>1 ? $2 : $1)' -- since you won't have to rewrite $0– glenn jackman
yesterday
@glennjackman Well, the
cut solution would be even faster in any case.– Kusalananda♦
yesterday
@glennjackman Well, the
cut solution would be even faster in any case.– Kusalananda♦
yesterday
add a comment |
awk -F, 'NF == 1 print $1
NF > 1 print $2' filename
This will print just the first string if there is no comma, second string if there is one or more comma.
add a comment |
awk -F, 'NF == 1 print $1
NF > 1 print $2' filename
This will print just the first string if there is no comma, second string if there is one or more comma.
add a comment |
awk -F, 'NF == 1 print $1
NF > 1 print $2' filename
This will print just the first string if there is no comma, second string if there is one or more comma.
awk -F, 'NF == 1 print $1
NF > 1 print $2' filename
This will print just the first string if there is no comma, second string if there is one or more comma.
answered yesterday
unxnutunxnut
3,80721120
3,80721120
add a comment |
add a comment |
You can do this with Perl as follows.
Command-line:
$ perl -F, -pale '$_ = $F[1] // $_' out.txt
Explanation:
-pwill read records line-by-line AND autoprint before going in to read the next or eof.-lmakesIRS = ORS = "n"-F,makesFSa comma.-asplits each record$_on the field separator, in our case a comma, and goes ahead and stores the fields so generated in the array@F, which is zero-indexed.-eimplies, what follows it is thePerlcode, which shall be gets applied to each record.$_ = $F[1] // $_expression reads as follows: if the 2nd field$F[1]isn't defined, use the current record$_. And then the result of this expression is assigned to the current record$_.- owing to the
-pswitch ofperlbeing in use, before the new record is read in, the current record is taken tostdout.
Result:
NA
NA
NA
NA
NA
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85752
gene85752
You may also do it with the GNU version of the sed editor as shown below:
$ sed -ne '
s/,/n/
s/.*n//
s/,/n/
P
' out.txt
add a comment |
You can do this with Perl as follows.
Command-line:
$ perl -F, -pale '$_ = $F[1] // $_' out.txt
Explanation:
-pwill read records line-by-line AND autoprint before going in to read the next or eof.-lmakesIRS = ORS = "n"-F,makesFSa comma.-asplits each record$_on the field separator, in our case a comma, and goes ahead and stores the fields so generated in the array@F, which is zero-indexed.-eimplies, what follows it is thePerlcode, which shall be gets applied to each record.$_ = $F[1] // $_expression reads as follows: if the 2nd field$F[1]isn't defined, use the current record$_. And then the result of this expression is assigned to the current record$_.- owing to the
-pswitch ofperlbeing in use, before the new record is read in, the current record is taken tostdout.
Result:
NA
NA
NA
NA
NA
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85752
gene85752
You may also do it with the GNU version of the sed editor as shown below:
$ sed -ne '
s/,/n/
s/.*n//
s/,/n/
P
' out.txt
add a comment |
You can do this with Perl as follows.
Command-line:
$ perl -F, -pale '$_ = $F[1] // $_' out.txt
Explanation:
-pwill read records line-by-line AND autoprint before going in to read the next or eof.-lmakesIRS = ORS = "n"-F,makesFSa comma.-asplits each record$_on the field separator, in our case a comma, and goes ahead and stores the fields so generated in the array@F, which is zero-indexed.-eimplies, what follows it is thePerlcode, which shall be gets applied to each record.$_ = $F[1] // $_expression reads as follows: if the 2nd field$F[1]isn't defined, use the current record$_. And then the result of this expression is assigned to the current record$_.- owing to the
-pswitch ofperlbeing in use, before the new record is read in, the current record is taken tostdout.
Result:
NA
NA
NA
NA
NA
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85752
gene85752
You may also do it with the GNU version of the sed editor as shown below:
$ sed -ne '
s/,/n/
s/.*n//
s/,/n/
P
' out.txt
You can do this with Perl as follows.
Command-line:
$ perl -F, -pale '$_ = $F[1] // $_' out.txt
Explanation:
-pwill read records line-by-line AND autoprint before going in to read the next or eof.-lmakesIRS = ORS = "n"-F,makesFSa comma.-asplits each record$_on the field separator, in our case a comma, and goes ahead and stores the fields so generated in the array@F, which is zero-indexed.-eimplies, what follows it is thePerlcode, which shall be gets applied to each record.$_ = $F[1] // $_expression reads as follows: if the 2nd field$F[1]isn't defined, use the current record$_. And then the result of this expression is assigned to the current record$_.- owing to the
-pswitch ofperlbeing in use, before the new record is read in, the current record is taken tostdout.
Result:
NA
NA
NA
NA
NA
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85753
gene85752
gene85752
You may also do it with the GNU version of the sed editor as shown below:
$ sed -ne '
s/,/n/
s/.*n//
s/,/n/
P
' out.txt
answered 16 hours ago
Rakesh SharmaRakesh Sharma
262
262
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%2f511284%2fextract-characters-between-two-commas%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