LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Print % of the lines in a file (https://www.linuxquestions.org/questions/linux-newbie-8/print-of-the-lines-in-a-file-4175508746/)

papori 06-21-2014 07:09 PM

Print % of the lines in a file
 
Hi all,
I have multiple files(10000+), which i want to print 95% of the total lines in each file.
For example if i have file with 100 lines, i want to print the first 95.
My problem is that this step happen in a middle of a script..

Is there an easy way to do so?

Thanks,
Pap

chrism01 06-22-2014 03:15 AM

Show us what code you've tried so far and what exact problem you have.
We're here to help rather than write code for you.

papori 06-22-2014 03:47 AM

i wrote this:
Code:

#!/bin/bash

FILE=$1

i=1
l=`wc -l < $FILE`
x=$($l * 0.95 | bc) #the 0.95 is just example
while read line; do
i+=1
if [ i < x ]; then
  echo $i
fi
done < $FILE


papori 06-22-2014 09:20 AM

Anyway - this works for me:

Code:

#!/bin/bash

FILE=$1
i=1
l=($(wc -l $1))# count lines from the $1
a=0.95 #duplicate factor

c="$(echo "$a * $l" | bc)" # calculation

while read line
do
(( i++ ))
if (( $(echo "$i < $c" | bc -l) )); then
echo $i #or any other cmd
fi
done < $FILE



All times are GMT -5. The time now is 08:15 AM.