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

                                Mobil Contents History Mobil brands Former Mobil brands Lukoil transaction Mobil UK Mobil Australia Mobil New Zealand Mobil Greece Mobil in Japan Mobil in Canada Mobil Egypt See also References External links Navigation menuwww.mobil.com"Mobil Corporation"the original"Our Houston campus""Business & Finance: Socony-Vacuum Corp.""Popular Mechanics""Lubrite Technologies""Exxon Mobil campus 'clearly happening'""Toledo Blade - Google News Archive Search""The Lion and the Moose - How 2 Executives Pulled off the Biggest Merger Ever""ExxonMobil Press Release""Lubricants""Archived copy"the original"Mobil 1™ and Mobil Super™ motor oil and synthetic motor oil - Mobil™ Motor Oils""Mobil Delvac""Mobil Industrial website""The State of Competition in Gasoline Marketing: The Effects of Refiner Operations at Retail""Mobil Travel Guide to become Forbes Travel Guide""Hotel Rankings: Forbes Merges with Mobil"the original"Jamieson oil industry history""Mobil news""Caltex pumps for control""Watchdog blocks Caltex bid""Exxon Mobil sells service station network""Mobil Oil New Zealand Limited is New Zealand's oldest oil company, with predecessor companies having first established a presence in the country in 1896""ExxonMobil subsidiaries have a business history in New Zealand stretching back more than 120 years. We are involved in petroleum refining and distribution and the marketing of fuels, lubricants and chemical products""Archived copy"the original"Exxon Mobil to Sell Its Japanese Arm for $3.9 Billion""Gas station merger will end Esso and Mobil's long run in Japan""Esso moves to affiliate itself with PC Optimum, no longer Aeroplan, in loyalty point switch""Mobil brand of gas stations to launch in Canada after deal for 213 Loblaws-owned locations""Mobil Nears Completion of Rebranding 200 Loblaw Gas Stations""Learn about ExxonMobil's operations in Egypt""Petrol and Diesel Service Stations in Egypt - Mobil"Official websiteExxon Mobil corporate websiteMobil Industrial official websiteeeeeeeeDA04275022275790-40000 0001 0860 5061n82045453134887257134887257

                                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