I would like to copy part of huge text file (tens of GB) into new smaller file, starting from the certain offset in percentage to the end, or from beginning 5%. Can that be done with a simple command in Windows?
Asked
Active
Viewed 4,586 times
1 Answers
6
If you have Windows 10, you can use Ubuntu-Bash cmd otherwise you might want to use Unix-GNU-Utils-for-Windows
Once you'll install it, you'll be able to use unix head and tail commands, and redirect the output into a new file
head -100(or any number of lines)tail -100(or any number of lines)
In order to get the amount of lines in the file, you can use Unix wc -l command
wc -l filename.txt
After you get the number of lines in this file, you can multiple the number with 5/100 in order to get the amount of 5%, and use this result in the head or tail commands
e.g.
head -100000 file1 > file2
head - output the first part of files -n, --lines=[-]K print the first K lines instead of the first 10; with the leading '-', print all but the last K lines of each file
tail - output the last part of files -n, --lines=K output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth
wc - print newline, word, and byte counts for each file -l, --lines print the newline counts
Yaron
- 724
- 2
- 7
- 14
-
1Any way to specify percentage? – Pablo May 03 '17 at 09:48
-
@pablo - you can use `wc -l` to get the length of the huge-file, and calculate the percentage using num-of-lines * (5/100) – Yaron May 03 '17 at 09:56
-
1@Yaron That will only be accurate if all of the lines are the same length ... – DavidPostill May 03 '17 at 22:57