0

I have two text files, file_1 and file_2. How can I concatenate file_2, from line 20 onwards, with file_1?

  • What do you mean by "concatenate"? – Satō Katsura Apr 6 '17 at 17:43
  • if i do: cat file_1 file_2 if combines both files completely. i don't want to add the entire file_2 to file_1, just from line 20 onwards... – Sushma Sharma Apr 6 '17 at 17:50
  • 1
    Then you want something like this: { cat file_1; sed 1,19d file_2 } >result. – Satō Katsura Apr 6 '17 at 17:51
0

Use ed, man! !man ed!

ed -s file_1 <<< $'r !sed -e 1,19d file_2\nwq'
  • -s = suppress diagnostic output
  • <<< = redirect ed's input to be the following here-string
  • $' ... ' = supply the following quoted string
  • r = tell ed to read from the following ...
  • !sed -e 1,19d file_2 = command, instead of a file; ask sed to show us everything except lines 1 through 19 (delete them) of file_2
  • wq = write and quit!
  • Also works with ex (no need for the -s there) – Jeff Schaller Apr 6 '17 at 18:07
  • grt explanation! thx! – Sushma Sharma Apr 6 '17 at 18:21
0

If I understand your question correctly, you want lines 20 onwards from file_2, followed by file_1's contents. I would do this:

( tail -n +20 file_2; cat file_1 ) > file_3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.