Concatenate files


 
Thread Tools Search this Thread
# 1  
Concatenate files

Hi
I am trying to learn linux step by step an i am wondering
can i use cat command for concatenate files but i want to place context of file1 to a specific position in file2 place of file 2 and not at the end as it dose on default?
Thank you.
# 2  
You can't do that with cat. "cat file1 file2" always places file2 at the end of file1. cat does not look for intermediate positions within files.

Could you provide a short example of file1 and file2, and the context where you want to place one file within the other?
This User Gave Thanks to hanson44 For This Post:
# 3  
You need to use several commands and redirection to achieve what you want. One (amongst many) example were:
Code:
$ head -5 file2 >fileX;cat file1 >>fileX; tail -n+6 file2 >>fileX

, assuming you want to split file2 after line 5.
This User Gave Thanks to RudiC For This Post:
# 4  
Here's how I would suggest doing it, using following starting materials:
Code:
$ cat file1
aaa
bbb
ccc
ddd

$ cat file2
AAA
BBB
CCC
DDD

Read file2 into file1, after 2nd line of file1:
Code:
$ sed "2 r file2" file1
aaa
bbb
AAA
BBB
CCC
DDD
ccc
ddd

Read file2 into file1, after line with "ccc" in file1:
Code:
$ sed "/ccc/ r file2" file1
aaa
bbb
ccc
AAA
BBB
CCC
DDD
ddd

sed can be pretty cryptic when misused, but it is very clean and useful for many simple tasks, such as this kind.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenate 2 files data

Hi, I have one file as: $cat file1 abc pqr 123 def wxy 234 xyz ghi 567 and another file as $cat file2 345 456 987 I want the output of file2 to be appended in file1. And file 1 should look like: $cat file1 abc pqr 123 345 def wxy 234 456 xyz ghi 567 987 (6 Replies)
Discussion started by: Bhrigu
6 Replies

2. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies

3. UNIX for Dummies Questions & Answers

Concatenate Several Files to One

Hi All, Need your help. I will need to concatenate around 100 files but each end of the file I will need to insert my name DIRT1228 on each of the file and before the next file is added and arrived with just one file for all the 100files. Appreciate your time. Dirt (6 Replies)
Discussion started by: dirt1228
6 Replies

4. Shell Programming and Scripting

Concatenate files

I have a file named "file1" which has the following data 10000 20000 30000 And I have a file named "file2" which has the following data ABC DEF XYZ My output should be 10000ABC 20000DEF (3 Replies)
Discussion started by: bobby1015
3 Replies

5. Shell Programming and Scripting

Concatenate files

Hi, I want to create a batch(bash) file to combine 23 files together. These files have the same extension. I want the final file is save to a given folder. Once it is done it will delete the 23 files. Thanks for help. Need script. (6 Replies)
Discussion started by: zhshqzyc
6 Replies

6. Shell Programming and Scripting

Concatenate files

I have directory structure sales_only under which i have multiple directories for each dealer example: ../../../Sales_Only/xxx_Dealer ../../../Sales_Only/yyy_Dealer ../../../Sales_Only/zzz_Dealer Every day i have one file produce under each directory when the process runs. The requirement... (3 Replies)
Discussion started by: mohanmuthu
3 Replies

7. Shell Programming and Scripting

Concatenate columns from files

Hi, I have a file1.txt like this: and a file2.txt like this: I wat only append file2.txt to file1.txt and to have something like this: How could i do ? (2 Replies)
Discussion started by: AdminLew
2 Replies

8. Shell Programming and Scripting

Script to concatenate several files

I need a script to concatenate several files in one step, I have 3 header files say file.S, file.X and file.R, I need to concatenate these 3 header files to data files, say file1.S, file1.R, file1.X so that the header file "file.S" will be concatenated to all data files with .S extentions and so on... (3 Replies)
Discussion started by: docaia
3 Replies

9. UNIX for Dummies Questions & Answers

How to concatenate all files.

Hi, I'm totally new to Unix. I'm an MVS mainframer but ran into a situation where a Unix server I have available will help me. I want to be able to remotely connect to another server using FTP, login and MGET all files from it's root or home directory, logout, then login as a different user and do... (1 Reply)
Discussion started by: s80bob
1 Replies

10. UNIX for Advanced & Expert Users

Split n then concatenate files.

I have a file which has 350,000 records. I want all of these in 1 excel sheet file(around 6 worksheets) I used the below command to split it. split -64000 Creditratingsdata.xls credits Now i am having problems of appending into 1 excel file. I even went to the extent of trying. (cat... (4 Replies)
Discussion started by: praveen.pinto
4 Replies

Featured Tech Videos