Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

merge of files together

Status
Not open for further replies.

a222401

MIS
Nov 29, 2002
21
BE
Hi,

I'm new with awk and i search a easy way to merge 2 files in one.

file1:
line1
line2
line3
line4
line5
file2:
bbb
ccc

the result that i like to have

line1
line2
line3
bbb
ccc
line4
line5

please help me
 
Something like this ?
awk -v file2insert=file2 -v insertafter=3 '
{print}
NR==insertafter{while((getline<file2insert)>0)print}
' /path/to/file1

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
great PHV,

but can you give me some explanation
i don't understand everything of your code

thanks
 
awk -v file2insert=file2 -v insertafter=3 '
Launch awk with 2 variables initialised from the shell:
file2insert will be an awk variable containing the pathname of the file to merge
insertafter will be an awk variable containing the line number after which the merge must be done
{print}
print to the output file each line of the input file
NR==insertafter
If we have just read (and printed) the number of desired lines
{while((getline<file2insert)>0)print}
Loop reading each line of the desired file and printing them
' /path/to/file1
name of input file
Anyway: man awk

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
It is simpler to use sed :
[tt]
sed '3r file2' file1
[tt]



Jean Pierre.
 
Salut Jean-Pierre.
I agree, but we are in the AWK forum, aren't we ? :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top