LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2014, 08:09 PM   #1
papori
LQ Newbie
 
Registered: Feb 2011
Posts: 23

Rep: Reputation: 0
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
 
Old 06-22-2014, 04:15 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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.
 
Old 06-22-2014, 04:47 AM   #3
papori
LQ Newbie
 
Registered: Feb 2011
Posts: 23

Original Poster
Rep: Reputation: 0
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

Last edited by papori; 06-22-2014 at 04:53 AM.
 
Old 06-22-2014, 10:20 AM   #4
papori
LQ Newbie
 
Registered: Feb 2011
Posts: 23

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read and print nth multiple lines from a file bal_nair Programming 5 11-08-2013 01:11 AM
[SOLVED] PERL:How to print the lines of a opening file without '\n' in the end? jimtony Programming 2 11-21-2011 09:22 PM
[SOLVED] Using AWK to print out the first few lines of a txt file mskalak Linux - Newbie 3 07-27-2011 03:58 PM
print the file: between the lines viveksnv Programming 3 02-26-2008 04:06 PM
print a text file with long lines and line number added powah Linux - General 2 05-26-2006 03:02 PM


All times are GMT -5. The time now is 12:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration