Home > Archive > AWK > February 2005 > insert
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| narsman 2005-02-01, 8:55 pm |
| Hi all,
Let's say you have 2 text files called file_A & file_B. I want to insert
the contents of file_B somewhere in the middle of file_A. How do you do
this? The only thing I could think of is by using the sed-insert command.
Would 'awk' be easier?
Thanks,
Narsman
| |
| Ed Morton 2005-02-02, 3:56 am |
|
narsman wrote:
> Hi all,
>
> Let's say you have 2 text files called file_A & file_B. I want to insert
> the contents of file_B somewhere in the middle of file_A. How do you do
> this? The only thing I could think of is by using the sed-insert command.
> Would 'awk' be easier?
Don't know about easier, but this is one way:
awk 'NR==FNR{lines[++nr]=$0;next}
FNR==18 { for (i=1; i<=nr; i++) print lines[i] }
{ print }' file_B file_A
The above would insert file_B before line 18 of file_A. Wouldn't be a
great solution for large files given your storing the whole of file_B in
an array. There's various other awk solutions but:
<OT>
The equivalent to the above is trivial in shell:
head -17 file_A
cat file_B
tail +18 file_A
for more info on shell solutions, including sed or perl, go to
comp.unix.shell.
</OT>
Regards,
Ed.
| |
| John Savage 2005-02-04, 3:55 pm |
| "narsman" <narciso@compsus.com> writes:
>Let's say you have 2 text files called file_A & file_B. I want to insert
>the contents of file_B somewhere in the middle of file_A. How do you do
Depends on your "somewhere". To insert contents of file2 after line 3
of file1: sed '3r file2' file1
To insert between the third and fourth words on line 3, for example, would
be a bit more complicated in pure sed.
--
John Savage (my news address is not valid for email)
| |
| narsman 2005-02-04, 8:55 pm |
| Hey Guys,
Thanks for the help. It worked well.
| |
| William James 2005-02-04, 8:55 pm |
| BEGIN { ip=5;ARGV[ARGC++]=ARGV[1];iname=ARGV[2]}
NR<ip
FILENAME==iname {print;x=1;next}
x&&FNR>=ip
| |
| William James 2005-02-04, 8:55 pm |
| BEGIN {ip=5;ARGV[ARGC++]=ARGV[1]}
NR==ip {x=2;nextfile}
x-=FNR==1 || FNR>=ip
|
|
|
|
|