Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Bash command to insert contents of one file into a specific part of another file

  • 19-06-2019 10:21am
    #1
    Registered Users, Registered Users 2 Posts: 5,660 ✭✭✭


    cat file_to_insert
    1
    2
    cat original_file
    a
    b
    c
    


    I want to put the contents of file_to_insert on the original file between b and c

    So it would look like
    a
    b
    1
    2
    c
    


Comments

  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    $ split -l 2 original_file original_part_ && cat original_part_aa file_to_insert original_part_ab
    a
    b
    1
    2
    c
    


  • Registered Users, Registered Users 2 Posts: 10,910 ✭✭✭✭28064212


    sed "/^b$/r file_to_insert" original_file
    
    This will insert the contents of file_to_insert after every line that exactly matches 'b'.

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Use head/tail and grep to do this. Grep to find the line before/after the special section.

    https://linux.die.net/man/1/grep - look at the -A and -B options.


  • Registered Users, Registered Users 2 Posts: 5,660 ✭✭✭veryangryman


    Cheers y'all. Settled on the sed command


  • Registered Users, Registered Users 2 Posts: 5,660 ✭✭✭veryangryman


    One more query on this
    sed "/^${last_string}$/r ${tmp_symlinks_file_part_2}" /var/tmp/sym_links.ini.$$
    

    Is there a way to do the replacement at X lines after the instance of the last string.

    In my case, i want to do it 6 lines down


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Yes, with the grep -A or -B options.

    Use grep to extract the section you want to change, then pipe this to sed.


  • Registered Users, Registered Users 2 Posts: 5,660 ✭✭✭veryangryman


    srsly78 wrote: »
    Yes, with the grep -A or -B options.

    Sorry to sound silly but how to shoehorn that into the current command?


  • Registered Users, Registered Users 2 Posts: 5,660 ✭✭✭veryangryman


    Answering my own q
    last_details=$(grep -A8 ${last_string} /var/tmp/sym_links.ini.$$)
            sed "/^${last_details}$/r ${tmp_symlinks_file_part_2}" /var/tmp/sym_links.ini.$$ > /var/tmp/sym_links.ini.$$.tmp2
    


  • Registered Users, Registered Users 2 Posts: 5,660 ✭✭✭veryangryman


    Seems to have trouble doing a multi line sed (as the grep gives us a multiline variable)


    sed: -e expression #1, char 24: unterminated address regex

    last_details=$(grep -A7 ${last_string} /var/tmp/sym_links.ini.$$)
    sed "/^${last_details}$/r ${tmp_symlinks_file_part_2}" /var/tmp/sym_links.ini.$$ > /var/tmp/sym_links.ini.$$.tmp2
    


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Pipe grep -A to head -n 1 to get one line.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 5,660 ✭✭✭veryangryman


    Similar ish query
    sed "/^\[DWH_DBSPACES_LOCATION\]$/r ${sysmain_content}" /var/tmp/sym_links.ini.$$ > /var/tmp/sym_links.ini.$$.tmp1
    

    How to alter this so that [DWH_DBSPACES_LOCATION] appears at the end rathjer than the start of the replaced string


Advertisement