For example, we have N files (file1, file2, file3 …)
We need first 20% of them, the result directory should be like (file1_20, file2_20, file3_20 …).
I was thinking use wc
to get the lines of the file, then times 0.2
Then use head
to get 20% and then redirect to a new file, but i don't know how to automate it.
Best Answer
So creating a single example to work from:
We can grab the size of the file in bytes with
stat
:And then using
bc
we can multipy the size by .2However we get a float so lets convert it to an integer for
head
(dd
might work here too ):And finally the first twenty percent (give or take a byte) of file1:
Putting it together we could then do something like this
where
f
is a place holder for any items found in the directory in which the for loop is run that matchesfile*
which when done:
update (to grab 20% of lines):
To grab the first approximate 20% of lines we could replace
stat --printf %s "$f"
with:Since we are using
printf
andbc
we can effectively round up from.5
, however if a file is only 1 or 2 lines long it will miss them. So we would want to not only round up, but default to at least grabbing 1 line.