How to find and replace using sed on records starting with Specific numberReplace non-printable characters in...

Improve quality of image bars

Does inertia keep a rotating object rotating forever, or something else?

How to have a continuous player experience in a setting that's likely to favor TPKs?

Wordplay subtraction paradox

Kepler space telescope undetected planets

Credit card details stolen every 1-2 years. What am I doing wrong?

Is it rude to refer to janitors as 'floor people'?

How many bits in the resultant hash will change, if the x bits are changed in its the original input?

Generating a PIN from cryptographic bytes

Can a Resident Assistant be told to ignore a lawful order?'

What is the point of a constraint expression on a non-templated function?

Operation Unz̖̬̜̺̬a͇͖̯͔͉l̟̭g͕̝̼͇͓̪͍o̬̝͍̹̻

How to honestly answer questions from a girlfriend like "How did you find this place" without giving the impression I'm always talking about my exes?

A scene of Jimmy diversity

Increasing muscle power without increasing volume

What were the problems on the Apollo 11 lunar module?

Does the Intel 8085 CPU use real memory addresses?

How can the electric potential be zero at a point where the electric field isn't, if that field can give a test charge kinetic energy?

What does it actually mean to have two time dimensions?

How Can I Process Untrusted Data Sources Securely?

How can a drink contain 1.8 kcal energy while 0 g fat/carbs/protein?

Why do so many pure math PhD students drop out or leave academia, compared to applied mathematics PhDs?

Did Voldemort kill his father before finding out about Horcruxes?

Wordplay addition paradox



How to find and replace using sed on records starting with Specific number


Replace non-printable characters in perl and sedSed Replace at a specific group of positionsHow to replace variables strings with special characters in sedsearch and Replace substring in mac address in a text fileReplace a pattern in File1 and replace it with corresponding matched pattern + column in File2using sed to replace a string in files which are in different foldersAdd a new line (n) in front of every forward arrow (>)Replace string in specific line at specific position of fixed-length fileStorage disk info - Replace multiple Input values to output fileReplace substring of characters with awk and sed






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I Have a fixed width file with the below format



012019-06-03070005000799111160300000030XXXXXXX0700000000030  
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000207000991755007000000300
022019-06-030007276384I06000000000000107000991755007000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000407000991758407000000300
022019-06-030007276384I06000000000000307000991758407000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030


i need work on the lines starting with 02 and have to replace the characters from 20th position to 50th position with "MMMM" preserving the spaces till the 50th position.



My output should look like this



enter image description here



i tried sed -Ee '/^02"s/((.20).{30}/1$(printf "%-30s" MMMM)/"', which replaces on the records starting with 01 and 02 both. but I want to work on records starting with 02.










share|improve this question









New contributor



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




















  • the desired output should be cross-checked... It's not only doing for line starts with "02".

    – msp9011
    23 mins ago


















0















I Have a fixed width file with the below format



012019-06-03070005000799111160300000030XXXXXXX0700000000030  
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000207000991755007000000300
022019-06-030007276384I06000000000000107000991755007000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000407000991758407000000300
022019-06-030007276384I06000000000000307000991758407000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030


i need work on the lines starting with 02 and have to replace the characters from 20th position to 50th position with "MMMM" preserving the spaces till the 50th position.



My output should look like this



enter image description here



i tried sed -Ee '/^02"s/((.20).{30}/1$(printf "%-30s" MMMM)/"', which replaces on the records starting with 01 and 02 both. but I want to work on records starting with 02.










share|improve this question









New contributor



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




















  • the desired output should be cross-checked... It's not only doing for line starts with "02".

    – msp9011
    23 mins ago














0












0








0








I Have a fixed width file with the below format



012019-06-03070005000799111160300000030XXXXXXX0700000000030  
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000207000991755007000000300
022019-06-030007276384I06000000000000107000991755007000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000407000991758407000000300
022019-06-030007276384I06000000000000307000991758407000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030


i need work on the lines starting with 02 and have to replace the characters from 20th position to 50th position with "MMMM" preserving the spaces till the 50th position.



My output should look like this



enter image description here



i tried sed -Ee '/^02"s/((.20).{30}/1$(printf "%-30s" MMMM)/"', which replaces on the records starting with 01 and 02 both. but I want to work on records starting with 02.










share|improve this question









New contributor



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











I Have a fixed width file with the below format



012019-06-03070005000799111160300000030XXXXXXX0700000000030  
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000207000991755007000000300
022019-06-030007276384I06000000000000107000991755007000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276384I06000000000000407000991758407000000300
022019-06-030007276384I06000000000000307000991758407000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030


i need work on the lines starting with 02 and have to replace the characters from 20th position to 50th position with "MMMM" preserving the spaces till the 50th position.



My output should look like this



enter image description here



i tried sed -Ee '/^02"s/((.20).{30}/1$(printf "%-30s" MMMM)/"', which replaces on the records starting with 01 and 02 both. but I want to work on records starting with 02.







linux shell-script sed






share|improve this question









New contributor



Arun 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 question









New contributor



Arun 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 question




share|improve this question








edited 30 mins ago









msp9011

5,3695 gold badges42 silver badges69 bronze badges




5,3695 gold badges42 silver badges69 bronze badges






New contributor



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








asked 48 mins ago









ArunArun

11 bronze badge




11 bronze badge




New contributor



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




New contributor




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















  • the desired output should be cross-checked... It's not only doing for line starts with "02".

    – msp9011
    23 mins ago



















  • the desired output should be cross-checked... It's not only doing for line starts with "02".

    – msp9011
    23 mins ago

















the desired output should be cross-checked... It's not only doing for line starts with "02".

– msp9011
23 mins ago





the desired output should be cross-checked... It's not only doing for line starts with "02".

– msp9011
23 mins ago










1 Answer
1






active

oldest

votes


















0














Try this,



Option 1: As per your desired output



sed "s/(.{21})(.{30})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
022019-06-03000727638MMMM 7000000300
022019-06-03000727638MMMM 7000000300
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030
012019-06-03070005000MMMM 00000030




  • (.{21}) first 21 characters will be stored in 1


  • (.{30}) next 30 charecters will be stored in 2


  • (.{30}) remaining stored in 3

  • Then it will substitute 123 with 1$(printf "%-30s" MMMM)3


Option 2: As per your context



sed "/^02/ s/(.{19})(.{31})(.*)/1$(printf "%-30s" MMMM)3/" file
012019-06-03070005000799111160300000030XXXXXXX0700000000030
012019-06-03070005000799165030700000030XXXXXXX0700000000030
012019-06-03070005000799175500700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799175840700000030XXXXXXX0700000000030
022019-06-030007276MMMM 07000000300
022019-06-030007276MMMM 07000000300
012019-06-03070005000799194080700000030XXXXXXX0700000000030
012019-06-03070005000790035750700000030XXXXXXX0700000000030
012019-06-03070005000790036660700000030XXXXXXX0700000000030




  • /^02/ will do the replacement only if the line starts with "02"






share|improve this answer




























    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
    });


    }
    });






    Arun is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f530771%2fhow-to-find-and-replace-using-sed-on-records-starting-with-specific-number%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try this,



    Option 1: As per your desired output



    sed "s/(.{21})(.{30})(.*)/1$(printf "%-30s" MMMM)3/" file
    012019-06-03070005000MMMM 00000030
    012019-06-03070005000MMMM 00000030
    012019-06-03070005000MMMM 00000030
    022019-06-03000727638MMMM 7000000300
    022019-06-03000727638MMMM 7000000300
    012019-06-03070005000MMMM 00000030
    022019-06-03000727638MMMM 7000000300
    022019-06-03000727638MMMM 7000000300
    012019-06-03070005000MMMM 00000030
    012019-06-03070005000MMMM 00000030
    012019-06-03070005000MMMM 00000030




    • (.{21}) first 21 characters will be stored in 1


    • (.{30}) next 30 charecters will be stored in 2


    • (.{30}) remaining stored in 3

    • Then it will substitute 123 with 1$(printf "%-30s" MMMM)3


    Option 2: As per your context



    sed "/^02/ s/(.{19})(.{31})(.*)/1$(printf "%-30s" MMMM)3/" file
    012019-06-03070005000799111160300000030XXXXXXX0700000000030
    012019-06-03070005000799165030700000030XXXXXXX0700000000030
    012019-06-03070005000799175500700000030XXXXXXX0700000000030
    022019-06-030007276MMMM 07000000300
    022019-06-030007276MMMM 07000000300
    012019-06-03070005000799175840700000030XXXXXXX0700000000030
    022019-06-030007276MMMM 07000000300
    022019-06-030007276MMMM 07000000300
    012019-06-03070005000799194080700000030XXXXXXX0700000000030
    012019-06-03070005000790035750700000030XXXXXXX0700000000030
    012019-06-03070005000790036660700000030XXXXXXX0700000000030




    • /^02/ will do the replacement only if the line starts with "02"






    share|improve this answer






























      0














      Try this,



      Option 1: As per your desired output



      sed "s/(.{21})(.{30})(.*)/1$(printf "%-30s" MMMM)3/" file
      012019-06-03070005000MMMM 00000030
      012019-06-03070005000MMMM 00000030
      012019-06-03070005000MMMM 00000030
      022019-06-03000727638MMMM 7000000300
      022019-06-03000727638MMMM 7000000300
      012019-06-03070005000MMMM 00000030
      022019-06-03000727638MMMM 7000000300
      022019-06-03000727638MMMM 7000000300
      012019-06-03070005000MMMM 00000030
      012019-06-03070005000MMMM 00000030
      012019-06-03070005000MMMM 00000030




      • (.{21}) first 21 characters will be stored in 1


      • (.{30}) next 30 charecters will be stored in 2


      • (.{30}) remaining stored in 3

      • Then it will substitute 123 with 1$(printf "%-30s" MMMM)3


      Option 2: As per your context



      sed "/^02/ s/(.{19})(.{31})(.*)/1$(printf "%-30s" MMMM)3/" file
      012019-06-03070005000799111160300000030XXXXXXX0700000000030
      012019-06-03070005000799165030700000030XXXXXXX0700000000030
      012019-06-03070005000799175500700000030XXXXXXX0700000000030
      022019-06-030007276MMMM 07000000300
      022019-06-030007276MMMM 07000000300
      012019-06-03070005000799175840700000030XXXXXXX0700000000030
      022019-06-030007276MMMM 07000000300
      022019-06-030007276MMMM 07000000300
      012019-06-03070005000799194080700000030XXXXXXX0700000000030
      012019-06-03070005000790035750700000030XXXXXXX0700000000030
      012019-06-03070005000790036660700000030XXXXXXX0700000000030




      • /^02/ will do the replacement only if the line starts with "02"






      share|improve this answer




























        0












        0








        0







        Try this,



        Option 1: As per your desired output



        sed "s/(.{21})(.{30})(.*)/1$(printf "%-30s" MMMM)3/" file
        012019-06-03070005000MMMM 00000030
        012019-06-03070005000MMMM 00000030
        012019-06-03070005000MMMM 00000030
        022019-06-03000727638MMMM 7000000300
        022019-06-03000727638MMMM 7000000300
        012019-06-03070005000MMMM 00000030
        022019-06-03000727638MMMM 7000000300
        022019-06-03000727638MMMM 7000000300
        012019-06-03070005000MMMM 00000030
        012019-06-03070005000MMMM 00000030
        012019-06-03070005000MMMM 00000030




        • (.{21}) first 21 characters will be stored in 1


        • (.{30}) next 30 charecters will be stored in 2


        • (.{30}) remaining stored in 3

        • Then it will substitute 123 with 1$(printf "%-30s" MMMM)3


        Option 2: As per your context



        sed "/^02/ s/(.{19})(.{31})(.*)/1$(printf "%-30s" MMMM)3/" file
        012019-06-03070005000799111160300000030XXXXXXX0700000000030
        012019-06-03070005000799165030700000030XXXXXXX0700000000030
        012019-06-03070005000799175500700000030XXXXXXX0700000000030
        022019-06-030007276MMMM 07000000300
        022019-06-030007276MMMM 07000000300
        012019-06-03070005000799175840700000030XXXXXXX0700000000030
        022019-06-030007276MMMM 07000000300
        022019-06-030007276MMMM 07000000300
        012019-06-03070005000799194080700000030XXXXXXX0700000000030
        012019-06-03070005000790035750700000030XXXXXXX0700000000030
        012019-06-03070005000790036660700000030XXXXXXX0700000000030




        • /^02/ will do the replacement only if the line starts with "02"






        share|improve this answer















        Try this,



        Option 1: As per your desired output



        sed "s/(.{21})(.{30})(.*)/1$(printf "%-30s" MMMM)3/" file
        012019-06-03070005000MMMM 00000030
        012019-06-03070005000MMMM 00000030
        012019-06-03070005000MMMM 00000030
        022019-06-03000727638MMMM 7000000300
        022019-06-03000727638MMMM 7000000300
        012019-06-03070005000MMMM 00000030
        022019-06-03000727638MMMM 7000000300
        022019-06-03000727638MMMM 7000000300
        012019-06-03070005000MMMM 00000030
        012019-06-03070005000MMMM 00000030
        012019-06-03070005000MMMM 00000030




        • (.{21}) first 21 characters will be stored in 1


        • (.{30}) next 30 charecters will be stored in 2


        • (.{30}) remaining stored in 3

        • Then it will substitute 123 with 1$(printf "%-30s" MMMM)3


        Option 2: As per your context



        sed "/^02/ s/(.{19})(.{31})(.*)/1$(printf "%-30s" MMMM)3/" file
        012019-06-03070005000799111160300000030XXXXXXX0700000000030
        012019-06-03070005000799165030700000030XXXXXXX0700000000030
        012019-06-03070005000799175500700000030XXXXXXX0700000000030
        022019-06-030007276MMMM 07000000300
        022019-06-030007276MMMM 07000000300
        012019-06-03070005000799175840700000030XXXXXXX0700000000030
        022019-06-030007276MMMM 07000000300
        022019-06-030007276MMMM 07000000300
        012019-06-03070005000799194080700000030XXXXXXX0700000000030
        012019-06-03070005000790035750700000030XXXXXXX0700000000030
        012019-06-03070005000790036660700000030XXXXXXX0700000000030




        • /^02/ will do the replacement only if the line starts with "02"







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 mins ago

























        answered 10 mins ago









        msp9011msp9011

        5,3695 gold badges42 silver badges69 bronze badges




        5,3695 gold badges42 silver badges69 bronze badges






















            Arun is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Arun is a new contributor. Be nice, and check out our Code of Conduct.













            Arun is a new contributor. Be nice, and check out our Code of Conduct.












            Arun is a new contributor. Be nice, and check out our Code of Conduct.
















            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%2f530771%2fhow-to-find-and-replace-using-sed-on-records-starting-with-specific-number%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

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...