Computing.Net > Forums > Unix > Need help with simple UNIX script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Need help with simple UNIX script

Reply to Message Icon

Name: Eskiya
Date: November 6, 2002 at 08:08:17 Pacific
OS: w2k
CPU/Ram: 128
Comment:

Hi, does anybody know how to write a script to count the number of lines in the file given as the 1st parameter on the command line,
and if this is more than 17 to display the top few and last few lines of the file to the screen, otherwise to display all the file to the screen, one page at a time.

I've used the head and tale command but just couldn't get around it...

thanks



Sponsored Link
Ads by Google

Response Number 1
Name: jimbo
Date: November 6, 2002 at 08:46:41 Pacific
Reply:

This might be what you want:

#! /bin/ksh
FILE=$1

if [[ $# == 0 ]] ; then
print "Argument expected: file"
exit 1
fi

if [[ ! -f $FILE ]] ; then
print "File does not exist."
exit 1
fi

typeset -i LINES=`wc -l "$FILE" | awk '{print $1}'`

if (( $LINES > 17 )) ; then
head -5 $FILE #Change as needed
tail -5 $FILE #Change as needed
else
more $FILE
fi


-jim


0

Response Number 2
Name: Don Arnett
Date: November 6, 2002 at 08:47:47 Pacific
Reply:

I hope that I'm not doing your homework for you, or at least you'll take the time to learn from this if I am.


# Count the number of lines in the file
# The file is sent to wc using cat and a pipe
# because if you say "wc -l $1", you'll get
# the filename in the output also.
numLines=`cat $1 | wc -l`

# Test the number of lines counted
# and issue the desired output
if [ $numLines -gt 17 ]
then

echo "First 10 lines"
head -10 $1
echo
echo
echo "Last 10 lines"
tail -10 $1

else

cat $1

fi


0

Response Number 3
Name: jimbo
Date: November 6, 2002 at 08:54:03 Pacific
Reply:

< # because if you say "wc -l $1", you'll get
< # the filename in the output also.
< numLines=`cat $1 | wc -l`

thanks for the tip there. that was bugging me when trying to figure out why the test failed before i added the '| awk'. learn something new every day.

-jim


0

Response Number 4
Name: LANkrypt0
Date: November 6, 2002 at 10:18:13 Pacific
Reply:

Perfect script Don, but the only suggestion I have is to use a different tail/head number OR raise the line count specification.
Reason being, if you have a file that is 18 or 19 lines you are going to get an overlap in the display. You may also want to add a break in the head/tail to separate the two. Its purely asthetic, but will make it tons easier to read.

Just my 2cp.


0

Response Number 5
Name: Don Arnett
Date: November 6, 2002 at 11:28:21 Pacific
Reply:

LanK - Good point about the line counts. I didn't think of that.

What is 'cp'?

I'm sure it's related to '2 cents', but can't figure out what 'cp' could mean. My best guess is 'copper pennies'!


0

Related Posts

See More



Response Number 6
Name: LANkrypt0
Date: November 6, 2002 at 12:48:13 Pacific
Reply:

cp = copper pieces (Everquest Term[an Online Role Playing Game] :)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Cron daemon Hp-9000 ux k200



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Need help with simple UNIX script

Need help with a unix script www.computing.net/answers/unix/need-help-with-a-unix-script/4288.html

Need help with a search and replace logi www.computing.net/answers/unix/need-help-with-a-search-and-replace-logi/3296.html

HELP!!!! with Write a script. www.computing.net/answers/unix/help-with-write-a-script/4918.html