Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KSH to test the length of a line

Status
Not open for further replies.

ryanc2

MIS
Apr 18, 2003
73
0
0
US
been away from scripting for awhile and need help testing the length of a record.

Basically, I need to read a list of files and kick out the filenames for any with a record length of < an input variable.

Thanks in advance...
 
ryanc2

I'm sure we had a similar post a few weeks ago, it may be worth searching or going back through the last few weeks posts.

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Try expr, I've forgotten the exact syntax, but the AIX man pages give an example of exactly what you're after

Ceci n'est pas une signature
Columb Healy
 
In addition to expr, the ksh has it's own length operator:

Code:
#!/bin/ksh

var="abcd"
len=`expr "$var" : '.*'`

len=${#var}

 
was it the length of the filename that you needed to check or the length of lines within that file?

your post's title refers to line whereas the body mentions records. It occurs to me that the first step to a working program is a clear specification ... I'm sure I've heard that somewhere ... so:
[ul]
[li]are you reading files specified in a list or just the filenames?[/li]
[li]is aforementioned list on the command line or in a file or input interactively or are all three methods possible?[/li]
[li]are the records fixed length or variable?[/li]
[li]are the 'records' separated with a newline or some other character or do you need to be able to specify the record separator?[/li]
[/ul]
if you are reading files to determine line lengths my solution would entail using the awk function length()
 
sorry if I wasn't being clear. I need to count the length of each line in a fixed length format. The real problem I have is there are leading/trailing spaces in the file.

I was using a variation of:

while read line
do
echo "$line" | wc -c
done < $1

but really want to only print the filename, for files with a line length of less than a certain variable.
 
ryanc2 said:
sorry if I wasn't being clear. I need to count the length of each line in a fixed length format. The real problem I have is there are leading/trailing spaces in the file.

if all the lines are of 'fixed length', wouldn't all the lines be of the same length?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
that's the problem - a drive filled up, so we have some incomplete records. Looking for a way to identify the files that contain these bad records...
 
Will this do the job (assumes fixed record length of 60)?

Code:
#! /bin/ksh

RecordLength=60

if [ $# -lt 1 ]
then
  echo No files specified
else
  while [ $# -gt 0 ]
  do
    FileName=$1
    while read Line
    do
      if [ $( echo $Line | wc -c ) -lt $RecordLength ]
      then
        echo $FileName
        break
      fi
    done < $FileName
    shift
  done
fi
 
This may be faster:
To find files with records longer than MAX - no need to read all the lines of all the files and then count each line...

Code:
MAX=1024
(( MAX=MAX+1 ))
cd /to/your/dir
for i in *
do
 nbiglines=$(cut -c${MAX}- $i|grep -c -v "^$")
 if [ ${nbiglines} -gt 0 ]
 then
  echo $i
 fi
done


HTH,

p5wizard
 
Call me picky[smarty], p5, but ryan was looking for short lines [ponder]

Also, the cut program in your script will indeed have to read all lines in all files in order to determine where to start copying each line, i.e. after MAX chars.

And what's with the bizarre setting of MAX to 1025 by initializing it and then incremented it? [surprise]
 
I know the question said ksh, but how about:

awk 'length < 60 {print FILENAME; nextfile}' files...

Annihilannic.
 
Brillimerant, Annihilannic! This is what we pay you for [2thumbsup]
 
I humbly admit to misreading/misinterpreting the post [morning]

the idea begind incrementing the variable by 1 is:

I would put the code in a script and invoke it with a parameter of 1024 (looking for lines longer than 1024: column 1025 until the end)

And yes, "cut" would read through the whole file, but my script doesn't invoke a new 'wc -l' process for every line being read...

But that's all beside the point anyway because we're looking for "shorter than" lines...


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top