LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Looking for a shell script that prints a file only if it has more than 10 lines (https://www.linuxquestions.org/questions/linux-newbie-8/looking-for-a-shell-script-that-prints-a-file-only-if-it-has-more-than-10-lines-652663/)

djfog 06-30-2008 04:46 PM

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...

jiml8 06-30-2008 05:09 PM

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:]]

djfog 06-30-2008 06:13 PM

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.

djfog 06-30-2008 06:24 PM

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..

dv502 06-30-2008 08:19 PM

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.

djfog 07-01-2008 03:35 AM

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.

dv502 07-01-2008 11:29 AM

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.

djfog 07-01-2008 12:15 PM

Thanks dv502.You 've been very useful.

dv502 07-01-2008 12:23 PM

Glad to help...


All times are GMT -5. The time now is 09:28 AM.