Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

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

  • 19-06-2019 11:21AM
    #1
    Registered Users, Registered Users 2 Posts: 5,762 ✭✭✭


    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: 11,090 ✭✭✭✭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,762 ✭✭✭veryangryman


    Cheers y'all. Settled on the sed command


  • Registered Users, Registered Users 2 Posts: 5,762 ✭✭✭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,762 ✭✭✭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,762 ✭✭✭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,762 ✭✭✭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,762 ✭✭✭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