Problem with Script that writes max lines of a file - Any ideas how to fix?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with Script that writes max lines of a file - Any ideas how to fix?
# 1  
Old 01-23-2009
Problem with Script that writes max lines of a file - Any ideas how to fix?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Last edited by mmiller99; 03-02-2009 at 08:27 PM..
mmiller99
# 2  
Old 01-23-2009
You're setting $NUMBER equal to the first argument and then later in the script checking that the first argument is numerically zero. Is this intended? And if you are using an argument of 0, then that has to be a filename in the directory for the "linetest1=`wc -l < $NUMBER`" to work.
Change line 1 to
Code:
#!/bin/ksh -x

so you can see what each line is evaluated to before it's executed.

Jerry
# 3  
Old 01-23-2009
Place some echo commands with your variables between the if statements and see what happens.
You also may run your script in debug mode, add the following line below the line #!/bin/ksh:

Code:
set -x

Another solution is:

Code:
wc -l /yourdir/* | grep -v total | sort -n | tail -1

Regards
# 4  
Old 01-23-2009
xxxxxxxxxxxxxxxxxxxxxxxxx

Last edited by mmiller99; 03-02-2009 at 08:28 PM..
mmiller99
# 5  
Old 01-23-2009
NUMBER=$1
if [ $# -ne 1 ] # Verify that there is only one argument on the command line
then
echo "Error. Can only use 0 or 1 arguments."
echo "Usage: maxlines.sh [directory]."
exit 1
fi
flag=1
# If the number of positional parameters is equal to zero search through
# the current directory. No directory provided on the command line.
if [ $1 -eq 0 ]
then
for testfile in * # Loop through the entire directory (top to bottom)
do
echo $testfile
if [ -f $testfile ] # Test that the name is a file
then
linetest1=`wc -l < $testfile`
echo $linetest1
if [ $flag -eq 1 ]
then
startline=$linetest1
startfile=$testfile
flag=2
fi
#The script executes the following code after the first pass
if [ $startline -lt $linetest1 ]
then
startline=$linetest1
startfile=$testfile
fi
fi
done
echo "File $startfile has the most lines with $startline lines."
exit 0
fi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identify lines with wrong format in a file and fix

Gurus, I have a data file which has a certain number of columns say 101. It has one description column which contains foreign characters and due to this some times, those special characters are translated to new line character and resulting in failing the process. I am using the following awk... (4 Replies)
Discussion started by: tumsri
4 Replies

2. Solaris

OpenBSM not catching all file writes

I have a custom auditing class configured for these events but it doesn't seem to be catching it when I do a "echo hey > test.txt" Any ideas on why that is? ---------- Post updated at 06:04 PM ---------- Previous update was at 05:47 PM ---------- Scratch this topic, it looks like it's because... (0 Replies)
Discussion started by: thmnetwork
0 Replies

3. Shell Programming and Scripting

Help with script that reads and writes java console Minecraft

Hi I am looking for an easy way to lock game mode (0) for everyone included op on a Minecraft server. It can be a script that every time a player changes game to 1 the script changes back to 0. What the player writes is visible in the java console. I am not good at script programming and my... (0 Replies)
Discussion started by: MyMorris
0 Replies

4. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

5. Shell Programming and Scripting

Reading from a file a background program writes to

Hi! #!/usr/bin/env bash rm tmpcomm nc -v -u -l 444 | hexdump -b > tmpcomm while : do read l1 < tmpcomm read l2 < tmpcomm read l3 < tmpcomm read l4 < tmpcomm # do something doneI start netcat in the background and listen for an incoming conncetion. All incoming... (1 Reply)
Discussion started by: torax123
1 Replies

6. Shell Programming and Scripting

BASH script problem using find, ideas?

Hi, I'm trying to write a script to search through my computer and find all .jpg files and put them all in a directory. So far I have this: for i in `find /home -name '*.jpg' ` ; do mv $i home/allen/Pictures/PicturesFound ; done When I run it, I get this error (this is only part of it, it... (2 Replies)
Discussion started by: FortressPTH
2 Replies

7. UNIX for Dummies Questions & Answers

which process writes to file

Some process rewrites a file ( i'm hacked :) Can I somehow monitor which process does that? (2 Replies)
Discussion started by: hachik
2 Replies

8. Shell Programming and Scripting

fix a problem in this script

z=9 i=0 h=02 min=55 while do cat /home/barmecha/test | grep $h:$min >> /home/barmecha/file1 min=`expr $min + 1` if ; then h=`expr $h + 1` fi i=`expr $i + 1` done i have a log file with time wise log in it, this script help me to pull out logs of the give time interval...but the... (8 Replies)
Discussion started by: abhishek27
8 Replies

9. Programming

Need ideas how to attack this problem

I'm at a total loss how to attack this problem. I have a file that contains ab What I need to do is if 1)if the string "ab" doesn't contain a newline, I need to insert one back into the buffer. 2)If the file contains two consecutive blank lines, skip over it. Here is what I started ... (5 Replies)
Discussion started by: frequency8
5 Replies

10. Shell Programming and Scripting

sh script that reads/writes based upon contents of a file

Hi everyone, Ive got a quick question about the feasibility and any suggestions for a shell script. I can use sh or ksh, doesnt matter. Basically, Ive got an output file from a db2 command that looks like so: SCHEMA NAME CARD LEAF ELEAF LVLS ISIZE NDEL KEYS F4 F5 ... (3 Replies)
Discussion started by: rdudejr
3 Replies
Login or Register to Ask a Question