LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-30-2008, 04:46 PM   #1
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Rep: Reputation: 15
Looking for a shell script that prints a file only if it has more than 10 lines


Hello my friends...I'm quite new at the world of unix and I'm searching for a shell script to print only files that have more than 10 lines.I'd appreciate your help...
 
Old 06-30-2008, 05:09 PM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
This command line picks up any file with the suffix .txt that contains 11 or more lines.

wc -l *.txt | grep [0-9][0-9] | grep -v [[:space:]]10[[:space:]]

Note that this command line will also pick up any file that has a two digit or more number in the filename. Thus, it will pick up the one line file myfile27.txt.

This variant corrects that defect:

wc -l *.txt | grep [0-9][0-9] | grep -v [[:space:]]10[[:space:]] | grep -v [[:space:]][0-9][[:space:]]

Last edited by jiml8; 06-30-2008 at 05:15 PM.
 
Old 06-30-2008, 06:13 PM   #3
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks for your answer jiml8.It's quite useful,but what if I want to find files with no suffix at all..?Or it works only for txt.
Thanks in advance.
 
Old 06-30-2008, 06:24 PM   #4
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
OK.I found it

wc -l *.* | grep [0-9][0-9] | grep -v [[:space:]]10[[:space:]] | grep -v [[:space:]][0-9][[:space:]]

Another question though.If I want to give the name of a file so that the script will check if it has more than 10 lines and then print it,what should I do...?Thanks,in advance..
 
Old 06-30-2008, 08:19 PM   #5
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
Quote:
Another question though.If I want to give the name of a file so that the script will check if it has more than 10 lines and then print it,what should I do...?Thanks,in advance..
Is this homework?

Anyway, here it is. This only works with text/ascii files.

Code:
#!/bin/bash

while [ $1 ] ; do
lines=$(wc -l $1 | awk '{print $1}')

if [ $lines -ge 10 ]  ;  then
echo "$1 has $lines lines"
cat $1

else

echo "$1 has less than 10 lines"

fi
shift
done
All you do is name the script and pass the file(s) to check.

Example: scriptname file1 file2 etc...

or

scriptname *.txt
scriptname *

This is how it works. The while loop will read each file argument and store the output of wc -l for that file into the variable called lines.

The if statement will test if the number stored in the variable lines is 10 or greater. If true, then it will print the file name and its contents. If false, then it will echo that the file has less then 10 lines and not print its contents.

If you pass two or more file arguements, the shift command will move the next file into position one (i.e $1)

A more seasoned script writer could write a similar script or better with fewer lines. It's good as a start point. Anyway, good luck.

Last edited by dv502; 07-01-2008 at 09:16 PM.
 
Old 07-01-2008, 03:35 AM   #6
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks my friend.It's a really nice script.It's not quite a homework,but I'm trying to get rid of some c++ programs I have,cause I consider shell scripting much more easier.But,what If I'd like to write at the terminal
./script and then the script will ask me "give the name of the file..."
This is how my c++ program works.
 
Old 07-01-2008, 11:29 AM   #7
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
You're welcome.

Quote:
I'd like to write at the terminal
./script and then the script will ask me "give the name of the file..."
If you want to add user input, here is an example

Code:
#!/bin/bash
ls -l

echo -n "Enter a file to check: "
read file
lines=$(wc -l $file | awk '{print $1}')

if [ $lines -ge 10 ]  ;  then
echo "$file has $lines lines"
cat $file

else

echo "$file has less than 10 lines"

fi
The read command will read user input.

Last edited by dv502; 07-01-2008 at 11:45 AM.
 
Old 07-01-2008, 12:15 PM   #8
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks dv502.You 've been very useful.
 
Old 07-01-2008, 12:23 PM   #9
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
Glad to help...
 
  


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
Shell script to jump over lines horacioemilio Programming 8 12-02-2007 05:44 PM
insert lines in a function using shell script shyamdey Programming 1 08-30-2006 07:48 AM
shell script for expanding lines in a file b123coder Programming 1 05-25-2006 04:51 AM
How can I use a shell script to add and replace lines in a file? abefroman Programming 10 12-27-2005 05:05 PM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 09:25 PM

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

All times are GMT -5. The time now is 12:25 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