Replace the content of specific cells in a tsv file with the edited content of other cellsTerminal-based...

CDG baggage claim before or after immigration?

Minimize taxes now that I earn more

Would Taiwan and China's dispute be solved if Taiwan gave up being the Republic of China?

How do I extract code from an arduino?

I reverse the source code, you negate the output!

How to manage expenditure when billing cycles and paycheck cycles are not aligned?

Is Zack Morris's 'time stop' ability in "Saved By the Bell" a supernatural ability?

How is underwater propagation of sound possible?

Aligning two sets of equations with alignat?

How to ask a man to not take up more than one seat on public transport while avoiding conflict?

Is it really necessary to have 4 hours meeting in Sprint planning?

Is the sentence "何でも忘れた" correct?

Can Bless or Bardic Inspiration help a creature from rolling a 1 on a death save?

Asking an expert in your field that you have never met to review your manuscript

Circle divided by lines between a blue dots

Can Northern Ireland's border issue be solved by repartition?

What is the need of methods like GET and POST in the HTTP protocol?

Simulate a 1D Game-of-Life-ish Model

Is there an in-universe reason Harry says this or is this simply a Rowling mistake?

Are actors contractually obligated to certain things like going nude/ Sensual Scenes/ Gory Scenes?

Spectrum of a Subspace of Matrices

Gas leaking in base of new gas range?

What is a Heptagon Number™?

Should the pagination be reset when changing the order?



Replace the content of specific cells in a tsv file with the edited content of other cells


Terminal-based spreadsheets and wordprocessor?How to [constantly] read the last line of a file?Remove specific words within lines






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







0















I have a tsv file like this:



col1            col2           
etc ok
something NULL
category1 ok
randomtext NULL


I need to replace "NULL" with the content of the cell on the left plus a number 2. Something like: if content of cell matches the string "NULL", replace it with the content of the cell on its left and add a number 2:



col1            col2      
etc ok
something something2
category1 ok
randomtext randomtext2


Thanks










share|improve this question



























  • how about: perl -ape 's/tNULL/"t$F[0]2"/e' filename > altered_file

    – Theophrastus
    12 mins ago













  • It replaces NULL with only the first word of the left cell. How can it be tweaked to include all the content of the cell?

    – hola
    1 min ago


















0















I have a tsv file like this:



col1            col2           
etc ok
something NULL
category1 ok
randomtext NULL


I need to replace "NULL" with the content of the cell on the left plus a number 2. Something like: if content of cell matches the string "NULL", replace it with the content of the cell on its left and add a number 2:



col1            col2      
etc ok
something something2
category1 ok
randomtext randomtext2


Thanks










share|improve this question



























  • how about: perl -ape 's/tNULL/"t$F[0]2"/e' filename > altered_file

    – Theophrastus
    12 mins ago













  • It replaces NULL with only the first word of the left cell. How can it be tweaked to include all the content of the cell?

    – hola
    1 min ago














0












0








0








I have a tsv file like this:



col1            col2           
etc ok
something NULL
category1 ok
randomtext NULL


I need to replace "NULL" with the content of the cell on the left plus a number 2. Something like: if content of cell matches the string "NULL", replace it with the content of the cell on its left and add a number 2:



col1            col2      
etc ok
something something2
category1 ok
randomtext randomtext2


Thanks










share|improve this question
















I have a tsv file like this:



col1            col2           
etc ok
something NULL
category1 ok
randomtext NULL


I need to replace "NULL" with the content of the cell on the left plus a number 2. Something like: if content of cell matches the string "NULL", replace it with the content of the cell on its left and add a number 2:



col1            col2      
etc ok
something something2
category1 ok
randomtext randomtext2


Thanks







bash word-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 28 mins ago







hola

















asked 51 mins ago









holahola

52 bronze badges




52 bronze badges
















  • how about: perl -ape 's/tNULL/"t$F[0]2"/e' filename > altered_file

    – Theophrastus
    12 mins ago













  • It replaces NULL with only the first word of the left cell. How can it be tweaked to include all the content of the cell?

    – hola
    1 min ago



















  • how about: perl -ape 's/tNULL/"t$F[0]2"/e' filename > altered_file

    – Theophrastus
    12 mins ago













  • It replaces NULL with only the first word of the left cell. How can it be tweaked to include all the content of the cell?

    – hola
    1 min ago

















how about: perl -ape 's/tNULL/"t$F[0]2"/e' filename > altered_file

– Theophrastus
12 mins ago







how about: perl -ape 's/tNULL/"t$F[0]2"/e' filename > altered_file

– Theophrastus
12 mins ago















It replaces NULL with only the first word of the left cell. How can it be tweaked to include all the content of the cell?

– hola
1 min ago





It replaces NULL with only the first word of the left cell. How can it be tweaked to include all the content of the cell?

– hola
1 min ago










1 Answer
1






active

oldest

votes


















0
















With awk:



awk 'BEGIN{ OFS="t" } { if ($2=="NULL"){ $2=$1"2" } print }' file


If field $2 is NULL, assign field $1 and "2" to field $2. Then print the line.



With sed:



sed 's/^([^[:blank:]]*)([[:blank:]]*)NULL([[:blank:]]*)/12123/' file


Capture the first column, the following space and the (possible) space after column 2 in three groups and replace with group one, two, one + "2" and three.





share




























    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/4.0/"u003ecc by-sa 4.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%2f542539%2freplace-the-content-of-specific-cells-in-a-tsv-file-with-the-edited-content-of-o%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
















    With awk:



    awk 'BEGIN{ OFS="t" } { if ($2=="NULL"){ $2=$1"2" } print }' file


    If field $2 is NULL, assign field $1 and "2" to field $2. Then print the line.



    With sed:



    sed 's/^([^[:blank:]]*)([[:blank:]]*)NULL([[:blank:]]*)/12123/' file


    Capture the first column, the following space and the (possible) space after column 2 in three groups and replace with group one, two, one + "2" and three.





    share






























      0
















      With awk:



      awk 'BEGIN{ OFS="t" } { if ($2=="NULL"){ $2=$1"2" } print }' file


      If field $2 is NULL, assign field $1 and "2" to field $2. Then print the line.



      With sed:



      sed 's/^([^[:blank:]]*)([[:blank:]]*)NULL([[:blank:]]*)/12123/' file


      Capture the first column, the following space and the (possible) space after column 2 in three groups and replace with group one, two, one + "2" and three.





      share




























        0














        0










        0









        With awk:



        awk 'BEGIN{ OFS="t" } { if ($2=="NULL"){ $2=$1"2" } print }' file


        If field $2 is NULL, assign field $1 and "2" to field $2. Then print the line.



        With sed:



        sed 's/^([^[:blank:]]*)([[:blank:]]*)NULL([[:blank:]]*)/12123/' file


        Capture the first column, the following space and the (possible) space after column 2 in three groups and replace with group one, two, one + "2" and three.





        share













        With awk:



        awk 'BEGIN{ OFS="t" } { if ($2=="NULL"){ $2=$1"2" } print }' file


        If field $2 is NULL, assign field $1 and "2" to field $2. Then print the line.



        With sed:



        sed 's/^([^[:blank:]]*)([[:blank:]]*)NULL([[:blank:]]*)/12123/' file


        Capture the first column, the following space and the (possible) space after column 2 in three groups and replace with group one, two, one + "2" and three.






        share











        share


        share










        answered 13 secs ago









        FreddyFreddy

        7,8981 gold badge6 silver badges29 bronze badges




        7,8981 gold badge6 silver badges29 bronze badges


































            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%2f542539%2freplace-the-content-of-specific-cells-in-a-tsv-file-with-the-edited-content-of-o%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