System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

copying file content to memory

 
Piotr Kirklewski
Super Advisor

copying file content to memory

Hi there
How can I copy a text-file content to the memory without editing the file ?
For example there is 12000 lines of text in file "A" and I want to copy and paste all of it to file "B" between line 1 and 3 ?
Is there any way of doing that without manually copying all the 12000 lines ?
Jesus is the King
3 REPLIES
Steven Schweda
Honored Contributor

Re: copying file content to memory

> How can I copy a text-file content to the
> memory without editing the file ?

By "to the memory", do you mean "to another
file"?

> [...] between line 1 and 3 ?

What did you want to do with line 2, which is
currently between lines 1 and 3?

> Is there any way of doing that [...]

Depending on exactly what "that" is, there
are probably several ways. Here's a crude
way of doing something:

man cat
man sed

M=2
N=3
( sed -n -e "1,${M}p" A ; cat B ; sed -n -e "${N},\$p" A )

For example:

alp$ cat A
File A line 1.
File A line 2.
File A line 3.
File A line 4.

alp$ cat B
File B line 1.
File B line 2.
File B line 3.
File B line 4.

alp$ sed -n -e "1,${M}p" A
File A line 1.
File A line 2.

alp$ sed -n -e "${N},\$p" A
File A line 3.
File A line 4.

alp$ ( sed -n -e "1,${M}p" A ; cat B ; sed -n -e "${N},\$p" A )
File A line 1.
File A line 2.
File B line 1.
File B line 2.
File B line 3.
File B line 4.
File A line 3.
File A line 4.
dirk dierickx
Honored Contributor

Re: copying file content to memory

what about just using plain old head/tail commands?

head -2 file_a | tail -n 1 >> file_b
Steven Schweda
Honored Contributor

Re: copying file content to memory

> what about just using plain old head/tail
> commands?

Did you try your suggestion before posting
it? How well did it work for you?

Plain old "head" and "tail" commands can
extract parts of one file, but it may not be
immediately clear how you assemble the
pieces. (Even if you do get the right
pieces.)

And, of course, I seem to have reversed the
roles of "A" and "B" in my example, if I
understand the original question (which I
don't claim to do).