Reorder lines near the beginning of a huge text file (> 20G)

I am a vim user and can use some basic awk or bash commands. Now I have a text file (vcf) larger than 20G. I would like to move line # 69 to the line below # 66:

$less huge.vcf
...
    66 ##contig=<ID=9,length=124595110>                                                                                                                                                       
    67 ##contig=<ID=X,length=171031299>                                                                                                                                                       
    68 ##contig=<ID=Y,length=91744698>                                                                                                                                                        
    69 ##contig=<ID=MT,length=16299>
...

I wanted:

...
    66 ##contig=<ID=9,length=124595110>     
    67 ##contig=<ID=MT,length=16299>                                                                                                                                                  
    68 ##contig=<ID=X,length=171031299>                                                                                                                                                       
    69 ##contig=<ID=Y,length=91744698>                                                                                                                                                        
...

I tried to open and edit it using vim (the LargeFile plugin is installed), but still does not work very well.

+4
source share
2 answers

An easy approach is to copy the section that you want to edit from your file, change it in place, and then copy it back.

# extract the first hundred lines
head -n 100 huge.txt >start.txt

# modify that extracted subset
vim start.txt

# copy that section back into the beginning of larger file
dd if=start.txt of=huge.txt conv=notrunc

, , . - , start.txt , , .

+13

awk-:

$ awk 'NR>=3 && NR<=4{b=b (b==""?"":ORS) $0;next}1;NR==5 {print b}' file
...
    66 ##contig=<ID=9,length=124595110>
    69 ##contig=<ID=MT,length=16299>
    67 ##contig=<ID=X,length=171031299>
    68 ##contig=<ID=Y,length=91744698>
...

. 3 -> 67, 4 -> 68 5 -> 69 . inplace, i inplace GNU awk.

+1

Source: https://habr.com/ru/post/1677203/


All Articles