简体   繁体   中英

How to remove 10 % of the lines of a (large) file?

I have some files (some > 30) that I want to remove the first 10% of their lines (starting at beginning).

With the help of other Stack Overflow users, I've tried to do something like this:

declare -a t
declare -a z
for j in {0..31}; do
    t[$j]=$(wc -l  < h_$j)
    z[$j]=$(echo "${t[$i]}"/10 | bc)
    sed "1,${z[$j]}d" h_$j > hh_$j
done

But for some files, and I have no idea why, it doesn't work. I though about split , but I couldn't find any option that allow me to only remove the first 10 % without generating 10 different files with 10 % of the original file.

Using tail

This uses tail to remove the first 10% of lines from file :

tail -n+$(( $(wc -l <file) / 10 )) file

Using sed

sed -n "$(( $(wc -l <file) / 10 ))",'$ p' file

If you want to change the file in place, use sed's -i option:

sed -i -n "$(( $(wc -l <file) / 10 ))",'$ p' file

For non-GNU sed (OSX, etc), the option -i may require an argument specifying the extension of a back-up file.

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