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













1















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?










share|improve this question
























  • 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















1















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?










share|improve this question
























  • 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













1












1








1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 just awk '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






  • 1





    @jasonwryan: Or just awk '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










4 Answers
4






active

oldest

votes


















3














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"





share|improve this answer
































    2














    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





    share|improve this answer






























      1














      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 the slice 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 the n till there are two left. At which point of time we use the P uppercase p, command to print the first portion of the pattern space.





      share|improve this answer






























        0














        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"'





        share|improve this answer










        New contributor




        Deepika Reddy Billuri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




















        • (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










        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%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









        3














        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"





        share|improve this answer





























          3














          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"





          share|improve this answer



























            3












            3








            3







            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"





            share|improve this answer















            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"






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 18 '17 at 5:18

























            answered May 18 '17 at 5:08









            ThorThor

            11.9k13459




            11.9k13459























                2














                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





                share|improve this answer



























                  2














                  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





                  share|improve this answer

























                    2












                    2








                    2







                    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





                    share|improve this answer













                    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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 18 '17 at 1:38









                    Stephen RauchStephen Rauch

                    3,344101428




                    3,344101428





















                        1














                        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 the slice 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 the n till there are two left. At which point of time we use the P uppercase p, command to print the first portion of the pattern space.





                        share|improve this answer



























                          1














                          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 the slice 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 the n till there are two left. At which point of time we use the P uppercase p, command to print the first portion of the pattern space.





                          share|improve this answer

























                            1












                            1








                            1







                            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 the slice 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 the n till there are two left. At which point of time we use the P uppercase p, command to print the first portion of the pattern space.





                            share|improve this answer













                            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 the slice 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 the n till there are two left. At which point of time we use the P uppercase p, command to print the first portion of the pattern space.






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 18 '17 at 3:53







                            user218374




























                                0














                                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"'





                                share|improve this answer










                                New contributor




                                Deepika Reddy Billuri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.




















                                • (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















                                0














                                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"'





                                share|improve this answer










                                New contributor




                                Deepika Reddy Billuri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.




















                                • (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













                                0












                                0








                                0







                                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"'





                                share|improve this answer










                                New contributor




                                Deepika Reddy Billuri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.










                                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"'






                                share|improve this answer










                                New contributor




                                Deepika Reddy Billuri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.









                                share|improve this answer



                                share|improve this answer








                                edited 1 hour ago





















                                New contributor




                                Deepika Reddy Billuri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.









                                answered 1 hour ago









                                Deepika Reddy BilluriDeepika Reddy Billuri

                                12




                                12




                                New contributor




                                Deepika Reddy Billuri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.





                                New contributor





                                Deepika Reddy Billuri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.






                                Deepika Reddy Billuri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.












                                • (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
















                                (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

















                                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%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





















































                                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

                                Frič See also Navigation menuinternal link

                                Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                                fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox