how to remove last two "-delimited strings from each line in a large fileUNIX paste columns and insert zeros for all missing valuesHow to add words to an existing columnReplace data at specific positions in txt file using data from another fileSelect certain column of of each file, paste to a new filesort CSV by number of column in rows?Sed and BBedit HtmlHow to sort by 2 columns and keep the top row from each group created?Extracting columns from a text file with no delimitersHow to use Unix Shell to show only the first n columns and last n columns?Speed up script that determines if all columns in a row are the same or not
Issue with units for a rocket nozzle throat area problem
Who has more? Ireland or Iceland?
Too soon for a plot twist?
Generating a list with duplicate entries
ESPP--any reason not to go all in?
Is the differential, dp, exact or not?
How do you use environments that have the same name within a single latex document?
3.5% Interest Student Loan or use all of my savings on Tuition?
Short SF story. Females use stingers to implant eggs in yearfathers
Propulsion Systems
Was it really inappropriate to write a pull request for the company I interviewed with?
How spaceships determine each other's mass in space?
Boss Telling direct supervisor I snitched
Precision notation for voltmeters
Why aren't there more Gauls like Obelix?
What do you call someone who likes to pick fights?
Rationale to prefer local variables over instance variables?
Does an unused member variable take up memory?
direct sum of representation of product groups
Can I challenge the interviewer to give me a proper technical feedback?
How can I portion out frozen cookie dough?
Help! My Character is too much for her story!
Limpar string com Regex
Why do phishing e-mails use faked e-mail addresses instead of the real one?
how to remove last two "-delimited strings from each line in a large file
UNIX paste columns and insert zeros for all missing valuesHow to add words to an existing columnReplace data at specific positions in txt file using data from another fileSelect certain column of of each file, paste to a new filesort CSV by number of column in rows?Sed and BBedit HtmlHow to sort by 2 columns and keep the top row from each group created?Extracting columns from a text file with no delimitersHow to use Unix Shell to show only the first n columns and last n columns?Speed up script that determines if all columns in a row are the same or not
I have numerous 2GB space-delimited files from a source system. Each row in each file contains the same number of strings surrounded by " as text qualifiers.
I need to eliminate the last two strings and their text qualifiers from every row in each file. (like removing the last two columns from a columnar report). With smaller files, I can import into Excel, delimit, delete the columns, save as tab delimited (much more useful than spaces).
Anycase, these files are too large and have too many rows for excel. So sed
??
"text1" "text2" "text3" "text4" "text5" "text6"
Every row has the same number of strings. How do I drop "text5" "text6" from every row?
text-processing sed text delete
add a comment |
I have numerous 2GB space-delimited files from a source system. Each row in each file contains the same number of strings surrounded by " as text qualifiers.
I need to eliminate the last two strings and their text qualifiers from every row in each file. (like removing the last two columns from a columnar report). With smaller files, I can import into Excel, delimit, delete the columns, save as tab delimited (much more useful than spaces).
Anycase, these files are too large and have too many rows for excel. So sed
??
"text1" "text2" "text3" "text4" "text5" "text6"
Every row has the same number of strings. How do I drop "text5" "text6" from every row?
text-processing sed text delete
awk '$5=$6=""1' file
...
– jasonwryan
May 18 '17 at 1:41
1
@jasonwryan: Or justawk 'NF=4'
– Thor
May 18 '17 at 5:04
@Thor better...
– jasonwryan
May 18 '17 at 5:07
add a comment |
I have numerous 2GB space-delimited files from a source system. Each row in each file contains the same number of strings surrounded by " as text qualifiers.
I need to eliminate the last two strings and their text qualifiers from every row in each file. (like removing the last two columns from a columnar report). With smaller files, I can import into Excel, delimit, delete the columns, save as tab delimited (much more useful than spaces).
Anycase, these files are too large and have too many rows for excel. So sed
??
"text1" "text2" "text3" "text4" "text5" "text6"
Every row has the same number of strings. How do I drop "text5" "text6" from every row?
text-processing sed text delete
I have numerous 2GB space-delimited files from a source system. Each row in each file contains the same number of strings surrounded by " as text qualifiers.
I need to eliminate the last two strings and their text qualifiers from every row in each file. (like removing the last two columns from a columnar report). With smaller files, I can import into Excel, delimit, delete the columns, save as tab delimited (much more useful than spaces).
Anycase, these files are too large and have too many rows for excel. So sed
??
"text1" "text2" "text3" "text4" "text5" "text6"
Every row has the same number of strings. How do I drop "text5" "text6" from every row?
text-processing sed text delete
text-processing sed text delete
edited May 18 '17 at 1:30
Stephen Rauch
3,344101428
3,344101428
asked May 18 '17 at 1:19
user231894user231894
61
61
awk '$5=$6=""1' file
...
– jasonwryan
May 18 '17 at 1:41
1
@jasonwryan: Or justawk 'NF=4'
– Thor
May 18 '17 at 5:04
@Thor better...
– jasonwryan
May 18 '17 at 5:07
add a comment |
awk '$5=$6=""1' file
...
– jasonwryan
May 18 '17 at 1:41
1
@jasonwryan: Or justawk 'NF=4'
– Thor
May 18 '17 at 5:04
@Thor better...
– jasonwryan
May 18 '17 at 5:07
awk '$5=$6=""1' file
...– jasonwryan
May 18 '17 at 1:41
awk '$5=$6=""1' file
...– jasonwryan
May 18 '17 at 1:41
1
1
@jasonwryan: Or just
awk 'NF=4'
– Thor
May 18 '17 at 5:04
@jasonwryan: Or just
awk 'NF=4'
– Thor
May 18 '17 at 5:04
@Thor better...
– jasonwryan
May 18 '17 at 5:07
@Thor better...
– jasonwryan
May 18 '17 at 5:07
add a comment |
4 Answers
4
active
oldest
votes
If you know that you always want to delete the last two columns, this idiom can be used:
awk 'NF-=2' file
I noticed that this does not work with nawk
, not sure why. The portable way is to force the field splitting with `$1=$1:
awk 'NF-=2 $1=$1' file
Output:
"text1" "text2" "text3" "text4"
add a comment |
This sed
command will remove the last two space separated, quoted strings from the end of each line from file infile
and send the results to outfile
:
sed 's/ *"[^"]*" *"[^"]*" *$//' < infile > outfile
add a comment |
awk '$(NF-1)=$NF=""1' inp
perl -pale '$_ = "@F[0..@F-3]"' inp
sed -ne '
s/" "/"
"/g
:a
s/n/ /
/n.*n.*n/ba
P
' inp
Explanation:
awk
code nulls out the last and second-last fields and prints.- In
perl
fields are stored in@F
array and theslice
from 0th to third-last are selected and stored in the current line$_
. The double quotes are there to effect the array->string xformation and joined together by the$"
superglobal
whose default value is a space.-p
Perl option then carries the$_
value to the stdout. - In
sed
we first turn all the patterns" "
--->"n"
then we enter a loop where we take back then
till there are two left. At which point of time we use theP
uppercase p, command to print the first portion of the pattern space.
add a comment |
Printing every field till last-2..AWK provided us number of fields in a row using variable NF
echo "text1" "text2" "text3" "text4" "text5" "text6" | awk -v ORS="" '
for(i=1;i<=NF-2;i++)print $i, " " ; print "n"'
New contributor
(1) This will put two spaces after each word, including the last one (i.e., at the end of the line). (2) If you would useprintf
instead ofprint
, you wouldn’t need to changeORS
. (3) Why bother submitting this when much more elegant awk solutions have already been posted?
– G-Man
36 mins 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
);
);
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%2f365749%2fhow-to-remove-last-two-delimited-strings-from-each-line-in-a-large-file%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
If you know that you always want to delete the last two columns, this idiom can be used:
awk 'NF-=2' file
I noticed that this does not work with nawk
, not sure why. The portable way is to force the field splitting with `$1=$1:
awk 'NF-=2 $1=$1' file
Output:
"text1" "text2" "text3" "text4"
add a comment |
If you know that you always want to delete the last two columns, this idiom can be used:
awk 'NF-=2' file
I noticed that this does not work with nawk
, not sure why. The portable way is to force the field splitting with `$1=$1:
awk 'NF-=2 $1=$1' file
Output:
"text1" "text2" "text3" "text4"
add a comment |
If you know that you always want to delete the last two columns, this idiom can be used:
awk 'NF-=2' file
I noticed that this does not work with nawk
, not sure why. The portable way is to force the field splitting with `$1=$1:
awk 'NF-=2 $1=$1' file
Output:
"text1" "text2" "text3" "text4"
If you know that you always want to delete the last two columns, this idiom can be used:
awk 'NF-=2' file
I noticed that this does not work with nawk
, not sure why. The portable way is to force the field splitting with `$1=$1:
awk 'NF-=2 $1=$1' file
Output:
"text1" "text2" "text3" "text4"
edited May 18 '17 at 5:18
answered May 18 '17 at 5:08
ThorThor
11.9k13459
11.9k13459
add a comment |
add a comment |
This sed
command will remove the last two space separated, quoted strings from the end of each line from file infile
and send the results to outfile
:
sed 's/ *"[^"]*" *"[^"]*" *$//' < infile > outfile
add a comment |
This sed
command will remove the last two space separated, quoted strings from the end of each line from file infile
and send the results to outfile
:
sed 's/ *"[^"]*" *"[^"]*" *$//' < infile > outfile
add a comment |
This sed
command will remove the last two space separated, quoted strings from the end of each line from file infile
and send the results to outfile
:
sed 's/ *"[^"]*" *"[^"]*" *$//' < infile > outfile
This sed
command will remove the last two space separated, quoted strings from the end of each line from file infile
and send the results to outfile
:
sed 's/ *"[^"]*" *"[^"]*" *$//' < infile > outfile
answered May 18 '17 at 1:38
Stephen RauchStephen Rauch
3,344101428
3,344101428
add a comment |
add a comment |
awk '$(NF-1)=$NF=""1' inp
perl -pale '$_ = "@F[0..@F-3]"' inp
sed -ne '
s/" "/"
"/g
:a
s/n/ /
/n.*n.*n/ba
P
' inp
Explanation:
awk
code nulls out the last and second-last fields and prints.- In
perl
fields are stored in@F
array and theslice
from 0th to third-last are selected and stored in the current line$_
. The double quotes are there to effect the array->string xformation and joined together by the$"
superglobal
whose default value is a space.-p
Perl option then carries the$_
value to the stdout. - In
sed
we first turn all the patterns" "
--->"n"
then we enter a loop where we take back then
till there are two left. At which point of time we use theP
uppercase p, command to print the first portion of the pattern space.
add a comment |
awk '$(NF-1)=$NF=""1' inp
perl -pale '$_ = "@F[0..@F-3]"' inp
sed -ne '
s/" "/"
"/g
:a
s/n/ /
/n.*n.*n/ba
P
' inp
Explanation:
awk
code nulls out the last and second-last fields and prints.- In
perl
fields are stored in@F
array and theslice
from 0th to third-last are selected and stored in the current line$_
. The double quotes are there to effect the array->string xformation and joined together by the$"
superglobal
whose default value is a space.-p
Perl option then carries the$_
value to the stdout. - In
sed
we first turn all the patterns" "
--->"n"
then we enter a loop where we take back then
till there are two left. At which point of time we use theP
uppercase p, command to print the first portion of the pattern space.
add a comment |
awk '$(NF-1)=$NF=""1' inp
perl -pale '$_ = "@F[0..@F-3]"' inp
sed -ne '
s/" "/"
"/g
:a
s/n/ /
/n.*n.*n/ba
P
' inp
Explanation:
awk
code nulls out the last and second-last fields and prints.- In
perl
fields are stored in@F
array and theslice
from 0th to third-last are selected and stored in the current line$_
. The double quotes are there to effect the array->string xformation and joined together by the$"
superglobal
whose default value is a space.-p
Perl option then carries the$_
value to the stdout. - In
sed
we first turn all the patterns" "
--->"n"
then we enter a loop where we take back then
till there are two left. At which point of time we use theP
uppercase p, command to print the first portion of the pattern space.
awk '$(NF-1)=$NF=""1' inp
perl -pale '$_ = "@F[0..@F-3]"' inp
sed -ne '
s/" "/"
"/g
:a
s/n/ /
/n.*n.*n/ba
P
' inp
Explanation:
awk
code nulls out the last and second-last fields and prints.- In
perl
fields are stored in@F
array and theslice
from 0th to third-last are selected and stored in the current line$_
. The double quotes are there to effect the array->string xformation and joined together by the$"
superglobal
whose default value is a space.-p
Perl option then carries the$_
value to the stdout. - In
sed
we first turn all the patterns" "
--->"n"
then we enter a loop where we take back then
till there are two left. At which point of time we use theP
uppercase p, command to print the first portion of the pattern space.
answered May 18 '17 at 3:53
user218374
add a comment |
add a comment |
Printing every field till last-2..AWK provided us number of fields in a row using variable NF
echo "text1" "text2" "text3" "text4" "text5" "text6" | awk -v ORS="" '
for(i=1;i<=NF-2;i++)print $i, " " ; print "n"'
New contributor
(1) This will put two spaces after each word, including the last one (i.e., at the end of the line). (2) If you would useprintf
instead ofprint
, you wouldn’t need to changeORS
. (3) Why bother submitting this when much more elegant awk solutions have already been posted?
– G-Man
36 mins ago
add a comment |
Printing every field till last-2..AWK provided us number of fields in a row using variable NF
echo "text1" "text2" "text3" "text4" "text5" "text6" | awk -v ORS="" '
for(i=1;i<=NF-2;i++)print $i, " " ; print "n"'
New contributor
(1) This will put two spaces after each word, including the last one (i.e., at the end of the line). (2) If you would useprintf
instead ofprint
, you wouldn’t need to changeORS
. (3) Why bother submitting this when much more elegant awk solutions have already been posted?
– G-Man
36 mins ago
add a comment |
Printing every field till last-2..AWK provided us number of fields in a row using variable NF
echo "text1" "text2" "text3" "text4" "text5" "text6" | awk -v ORS="" '
for(i=1;i<=NF-2;i++)print $i, " " ; print "n"'
New contributor
Printing every field till last-2..AWK provided us number of fields in a row using variable NF
echo "text1" "text2" "text3" "text4" "text5" "text6" | awk -v ORS="" '
for(i=1;i<=NF-2;i++)print $i, " " ; print "n"'
New contributor
edited 1 hour ago
New contributor
answered 1 hour ago
Deepika Reddy BilluriDeepika Reddy Billuri
12
12
New contributor
New contributor
(1) This will put two spaces after each word, including the last one (i.e., at the end of the line). (2) If you would useprintf
instead ofprint
, you wouldn’t need to changeORS
. (3) Why bother submitting this when much more elegant awk solutions have already been posted?
– G-Man
36 mins ago
add a comment |
(1) This will put two spaces after each word, including the last one (i.e., at the end of the line). (2) If you would useprintf
instead ofprint
, you wouldn’t need to changeORS
. (3) Why bother submitting this when much more elegant awk solutions have already been posted?
– G-Man
36 mins ago
(1) This will put two spaces after each word, including the last one (i.e., at the end of the line). (2) If you would use
printf
instead of print
, you wouldn’t need to change ORS
. (3) Why bother submitting this when much more elegant awk solutions have already been posted?– G-Man
36 mins ago
(1) This will put two spaces after each word, including the last one (i.e., at the end of the line). (2) If you would use
printf
instead of print
, you wouldn’t need to change ORS
. (3) Why bother submitting this when much more elegant awk solutions have already been posted?– G-Man
36 mins ago
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%2f365749%2fhow-to-remove-last-two-delimited-strings-from-each-line-in-a-large-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
awk '$5=$6=""1' file
...– jasonwryan
May 18 '17 at 1:41
1
@jasonwryan: Or just
awk 'NF=4'
– Thor
May 18 '17 at 5:04
@Thor better...
– jasonwryan
May 18 '17 at 5:07