Copying lines from one file to another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying lines from one file to another file
# 1  
Old 09-19-2011
Copying lines from one file to another file

I did a search before posting and couldn't find an example of what I need done. Anyway, I have a file called file1.txt, which has data like this:

Quote:
M977
57 62 78 94 96 107
M998
53 12 99 23 56 32
M1117
61 62 01 92 33 14
File2.txt contains:
Quote:
T55
12
M977
53 12
T90
01 88
M1117
33 14
So what I need to do is from file1.txt find the first line M977 in file2.txt and replace the line below it with the line from file1.txt. So file2.txt would look like this.

Quote:
T55
12
M977
57 62 78 94 96 107
T90
01 88
M1117
33 14
I would need to loop though file1.txt and continue though each line. The next pass would fine M998 in file2.txt and change the line below it with the line from file1.txt. Hope this makes sense.
# 2  
Old 09-19-2011
Code:
awk 'NR==FNR{x=$0;getline;a[x]=$0;next} 
{if($0 in a){x=$0;print x;print a[x];getline}else{print}}' file1 file2

--ahamed
# 3  
Old 09-19-2011
Code:
kent$  awk 'NR==FNR{if(NR%2)getline a[$0]} NR>FNR{if($0 in a){i=$0;print;next;} if(a[i]){$0= a[i];i=0} print $0}' file1.txt file2.txt
T55
12
M977
57 62 78 94 96 107
T90
01 88
M1117
61 62 01 92 33 14

# 4  
Old 09-19-2011
Hey guys, thanks for the reply. sk1418 - looks like yours is working, at least I can see it printing out the correct values. It's just that file2.txt isn't being over written with the new lines. How do you over write file2.txt to contain the new lines?
# 5  
Old 09-19-2011
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying a file to multiple other files using a text file as input

Hello, I have a file called COMPLIST as follows that contains 4 digit numbers.0002 0003 0010 0013 0015 0016 0022 0023 0024 0025 0027 0030 0031 0032 0033 0035 0038 0041 (3 Replies)
Discussion started by: sph90457
3 Replies

2. Shell Programming and Scripting

Copying few lines from one file to another

Hi all I have a requirement I need to copy only first 2 lines from a file of one location to another file of same location I used to code as cp head -2 abc 123 But i get the following error cp: 0653-437 123 is not a directory. The files used are cat abc ABCD EFDG TYUI (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

3. Shell Programming and Scripting

[Request] Copying a file with cp except for the last one or two lines.

Hi folks! I'm new to the unix-side of the world! ;) and I'm trying to learn as much as possible the good stuff that's in it! So.. here comes the point.. as you can see in the title, I need to copy a file but truncating it so that last 1-2 lines are not copied... any suggests from the... (6 Replies)
Discussion started by: WideMind
6 Replies

4. Shell Programming and Scripting

Copying Information from One File to Another File in Shell

Hello, I'm new to scripting and I need help moving text from one file to another file. Here are examples what the files look like. File 1: Ac-223 2.10m A 1 0 0 0 Fr-219 358 9.9000E-01 0 0.0 0 0.0 0 0.0... (1 Reply)
Discussion started by: tamachan414
1 Replies

5. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

6. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

7. Programming

Copying and overwriting a file using file descriptor

Hi , i have two basic requirement on linux platform . I am using C language to do this . 1) copying one file to another (assuming i know their file descriptors) 2) Overwriting a file using it file descriptor . Please guide. regards Aki (2 Replies)
Discussion started by: meet123321
2 Replies

8. UNIX for Dummies Questions & Answers

Copying common lines to a new file

How do i copy the common lines between 2 files to another file? for example file1: asdfg sdf gdsf file2: asdfgh asd sdf xcv file3: sdf (3 Replies)
Discussion started by: khestoi
3 Replies

9. Filesystems, Disks and Memory

Strange difference in file size when copying LARGE file..

Hi, Im trying to take a database backup. one of the files is 26 GB. I am using cp -pr to create a backup copy of the database. after the copying is complete, if i do du -hrs on the folders i saw a difference of 2GB. The weird fact is that the BACKUP folder was 2 GB more than the original one! ... (1 Reply)
Discussion started by: 0ktalmagik
1 Replies

10. Shell Programming and Scripting

copying the csv file into different worksheets of xls file

Hi, I have a script which will generate three csv files. i want to copy the contents of these csv files into a .XLS file but in different worksheets. Can a this be done in the same script? :confused: Can Perl come to my help in coping the csv files into different worksheets of .XLS file ?... (0 Replies)
Discussion started by: nimish
0 Replies
Login or Register to Ask a Question