简体   繁体   中英

to copy specific line numbers from one file to specific line numbers of another file using awk

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM