LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 07: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, 03:15 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 03: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 03:53 AM.
 
Old 06-22-2014, 09: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



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 12:11 AM
[SOLVED] PERL:How to print the lines of a opening file without '\n' in the end? jimtony Programming 2 11-21-2011 08:22 PM
[SOLVED] Using AWK to print out the first few lines of a txt file mskalak Linux - Newbie 3 07-27-2011 02:58 PM
print the file: between the lines viveksnv Programming 3 02-26-2008 03:06 PM
print a text file with long lines and line number added powah Linux - General 2 05-26-2006 02:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:34 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
Open Source Consulting | Domain Registration