How To Insert A File At A Specific Trace Of Piece Of Job Too Column

My objective is to insert the consummate contents of a text file at a specific row too column of closed to other text file.

If nosotros are simply concerned amongst inserting later a specific line, it tin last readily achieved amongst a release of Linux tools. For example, to insert file1.txt later the instant draw of file2.txt, whatever of the next commands volition do:

  • $ sed -i '2r file1.txt' file2.txt

  • $ awk '{print} NR==2 {while (getline < "file1.txt") print}' file2.txt

    Unlike the previous sed ascendance which modifies file2 in-line, the higher upwardly awk ascendance writes the desired output to measure output only.

  • $ emacs -batch +3 file2.txt --insert file1.txt -f save-buffer -kill

    This uses the batch capability of the emacs text editor. The ascendance opens file2 at draw 3, inserts file1, saves too and thence exits.

  • $ half dozen +2 file2.txt << DELIM
    > :r file1.txt
    > :wq
    > DELIM
    Vim: Warning: Input is non from a terminal

    Note that later yous come inwards the get-go draw ("vi +2 ..."), yous volition last prompted for to a greater extent than input. At that time, yous come inwards the side past times side 3 lines (:r, :wq, DELIM)

While Linux has many utilities to manipulate text (sed, perl, awk, cut, python), the easiest way I tin recall of to accomplish my objective to insert at a target draw too column is using emacs.

$ emacs -batch -Q +2:3 file2.txt --insert file1.txt -f save-buffer -kill 2>/dev/null

The primal is +2:3 which directs the editor to opened upwardly the file at draw two column 3.

Two other components inwards the higher upwardly emacs ascendance warrant closed to explanation. First, -Q agency quick startup. Quick, because emacs won't charge whatever init file, or whatever splash file. Second, the final business office of the ascendance pipes whatever measure fault output from the emacs editor to the cypher device.

I don't incertitude that sed, awk or perl tin create the job. If yous convey a uncomplicated solution, delight portion amongst us via the comment characteristic of this spider web page. Many thanks.

Berlangganan update artikel terbaru via email:

0 Response to "How To Insert A File At A Specific Trace Of Piece Of Job Too Column"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel