1
votes

I have a .txt file with 71 lines and I have another 12 set of files(file1 to file12). I want to copy first 5 lines from .txt file to file1 on specific line numbers similarly next 5 lines from .txt to file2 again on specific line numbers and so on.

This is my current code:

n = 1
sed -i '52,56d' $dumpfile 
awk'{print $'"$n"',$'"$n+1"',$'"$n+2"',$'"$n+3"'}' sample.txt > $dumpfile
n=$(($n + 1))

In $dumpfile I have put my 12 files.

Sample file (12files; file1, file2...)

...........  
................  
..............  
abc = 4,1,3  
def = 1,2,6  
dfg = 28,36,4  
tyu = 68,47,6  
rty = 65,6,97 

file (sample.txt)

abc = 1,2,3  
def = 4,5,6  
dfg = 2,3,4  
tyu = 8,7,6  
rty = 5,6,7

abc = 21,2,32  
def = 64,53,6  
dfg = 28,3,4  
tyu = 18,75,6  
rty = 5,63,75

...........  
...........  

I want to replace these five lines of (file1... file12) with five lines of sample.txt file. Line number of lines to be replaced in file1 to file12 are same in all the 12 files, where as in sample.txt file first set of 5 lines will go in file1, second set of 5 lines will go in file2 and so on upto file12.

1
there are many ways to do this. However, it would be a good start to see what you tried so far and what problems you found while doing it. Also, see How do I ask a good question? to ask properly.fedorqui 'SO stop harming'
split -l 5 input_file would be your best bet, although presumably you have been told to use awk as a learning exercise. If that's the case, I agree with @fedorqui, you really need to make an attempt yourself.Tom Fenech
Can you give some sample input and output?Sobrique
sample input file (.txt) abc = 1,2,3 def = 4,5,6 dfg = 2,3,4 tyu = 8,7,6 rty = 5,6,7 sample output file (12files, file1, file2) ................ .................. .............. abc = 4,1,3 def = 1,2,6 dfg = 28,36,4 tyu = 68,47,6 rty = 65,6,97 (I am not getting how to type properly)Helloo
@Helloo again, please edit your question to show this input. We cannot read code in commentsfedorqui 'SO stop harming'

1 Answers

0
votes

What you need is something like, this (uses GNU awk for ARGIND and inplace editing):

awk -i inplace -v start=52 '
NR==FNR {new[NR]=$0; next}
FNR==start {print new[ARGIND-1]; c=5}
!(c&&c--)
' RS="" sample.txt RS='\n' file1 file2 ... file12

but until you post some testable sample input and the associated output it's just a guess and, obviously, untested.