Inserting a file into another file above a specified location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting a file into another file above a specified location
# 1  
Old 10-20-2008
Bug Inserting a file into another file above a specified location

Hi.I have two files say XXX.dat and YYY.dat

The contents of XXX.dat and YYY.dat are as follows

XXX.dat

Jan
Feb
Mar
Apr
Method 1
{
echo "This is method 1"
}

YYY.dat

Delhi
Culcutta
One
Two
Three
Four
Five
Chennai
Mumbai

Now I want the insert only three lines of YYY.dat(line numbers 3 to 7) into XXX.dat above method 1(at line number 5)Method 1 may come anywhere in XXX.dat and the insertion into XXX.dat is purely dependent on method 1.Please give a solution based on the line number of method 1,as there may be many method 1 s and I want to insert only for the first occurance of method 1.Hopefully I want a solution based on line numbers,not based on patterns.Thank you,Smilie
# 2  
Old 10-20-2008
$ cat 1.awk
NR==FNR{if(NR>=3 && NR<=7) arr[i]=$0; i++; next}
{
if($0 == "Method 1") for (i in arr) print arr[i]
print
}

$ awk -f 1.awk YYY.dat XXX.dat
Jan
Feb
Mar
Apr
One
Two
Three
Four
Five
Method 1
{
echo "This is method 1"
}
# 3  
Old 10-20-2008
Tools Why the resubmit of essentially the same question?

https://www.unix.com/shell-programmin...ne-number.html
# 4  
Old 10-20-2008
no...The first one is inserting a file into another file after a line number whereas the new one is respect to inserting specified lines of a file above a line number(or a specified pattern).Thanks
# 5  
Old 10-21-2008
when match 'pat' on a.txt, insert the content of b.txt above matched line in a.txt.

Code:
open FH,"<b.txt";
while(<FH>){
	push(@arr,$_);
}
close FH;
open FH,"<a.txt";
while(<FH>){
	if(index($_,"pat")==0){
		for $item(@arr){
			print $item;
		}
		print $_;
	}
	else{
		print;
	}
}
close FH;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl to download file from subdivx.com after following location without knowing the file name/extens

This question could be specific to the site subdivx.com In the past, I've been able to download a file following location using cURL but there is something about subdivx.com that's different and can't figure out how to get it to work. I tried the following directly in the terminal with no... (5 Replies)
Discussion started by: MoonD
5 Replies

2. Shell Programming and Scripting

Inserting IDs from a text file into a sequence alignment file

Hi, I have one file with one column and several hundred entries File1: NA1 NA2 NA3And now I need to run a command within a mapping aligner tool to insert these sample names into a sequence alignment file (SAM) such that they look like this @RG ID:Library1 SM:NA1 PL:Illumina ... (7 Replies)
Discussion started by: nans
7 Replies

3. Shell Programming and Scripting

Shell script to read specified value from file and echo to the same location to other file.

Hello. I want to to backup some "default:" values from a file do some other job and after restore that "default:" values back. The problem is that the source and destination file has a lot of default: strings in it but with different values... So.. Here is an example: A part of my source... (6 Replies)
Discussion started by: ausdim
6 Replies

4. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

5. Shell Programming and Scripting

Asking user for input file location using a .sh file in windows system

Hi all, I am creating a .sh file in windows environment using notepad. i need a code which i can write in this .sh file so that it asks me for an input file stored anywhere in my C drive of my windows computer. Please help me out with this. (1 Reply)
Discussion started by: bansalpankaj88
1 Replies

6. Shell Programming and Scripting

How to copy a file from one location to another location?

I have file file1.txt in location 'loc1'. Now i want a copy of this file in location 'loc2' with a new file called test.txt. Please help me how to do this in shell script. (1 Reply)
Discussion started by: vel4ever
1 Replies

7. Shell Programming and Scripting

File created in a different location instead of desired location on using crontab

Hi, I am logging to a linux server through a user "user1" in /home directory. There is a script in a directory in 'root' for which all permissions are available including the directory. This script when executed creates a file in the directory. When the script is added to crontab, on... (1 Reply)
Discussion started by: archana.n
1 Replies

8. Shell Programming and Scripting

Put one string from one location to another location in a file

Hi Everyone, I have 1.txt here a b c' funny"yes"; d e The finally output is: here a b c d e' funny"yes"; (1 Reply)
Discussion started by: jimmy_y
1 Replies

9. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

10. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies
Login or Register to Ask a Question