Computing.Net > Forums > Unix > Seraching for records with length

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Seraching for records with length

Reply to Message Icon

Original Message
Name: anilcgowda
Date: September 1, 2005 at 11:31:16 Pacific
Subject: Seraching for records with length
OS: UNIX
CPU/Ram: 1GB
Comment:

Hi,

Can you please let me know how to search records in a file which are greater than length 1000.

Thanks,
Anil


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: September 1, 2005 at 12:55:12 Pacific
Reply:

In ksh, ${#parameter} is the length of parameter:

#!/bin/ksh

while read line
do
if [[ ${#line} > 1000 ]]
then
echo ${line}
fi
done < myfile


Report Offensive Follow Up For Removal

Response Number 2
Name: anilcgowda
Date: September 1, 2005 at 15:38:33 Pacific
Reply:

Hi,

The above code displays all the records in the file - even though their length is less than 1000.

Do I have to specify anything in the place of #

Thanks,
Anil


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: September 1, 2005 at 16:23:40 Pacific
Reply:

I don't know what to tell you; it works for me just the way it is written. Are you sure you are using ksh.

You can use the expr command to also get the length:

#!/bin/ksh

while read line
do

if [[ $(expr "${line}" : '.*') > 1000 ]]
then
echo ${line}
fi
done < myfile


Report Offensive Follow Up For Removal

Response Number 4
Name: Jim Boothe
Date: September 4, 2005 at 12:04:08 Pacific
Reply:

The first solution works for me on HP-UX only if I use -gt or -ge. Also, you will want to set IFS to null to prevent echo from collapsing any amount of white space to single spaces - in other words, to preserve your line exactly as it is.

#!/bin/ksh

IFS=

while read line
do
if [[ ${#line} -gt 9 ]]
then
echo ${line}
fi
done < myfile


Report Offensive Follow Up For Removal







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



Results for: Seraching for records with length

appending records with sub category
    Summary: Currently it is all one field and has little format (spaces do not delimit the record). The Apple, Bear etc may be two words such as "red apple" that will become the first field. I just used the ...
www.computing.net/answers/unix/appending-records-with-sub-category/6423.html

awk script for search and replace
    Summary: I have a file with thousands of records in it. Each record in the file being of a fixed length of 190 bytes. Example: 0003006315B000000002100601X646000000054717154-12700911700 00Tel...
www.computing.net/answers/unix/awk-script-for-search-and-replace/7039.html

Appending record with same field
    Summary: Well Nails this works for a shorter record but if record length exceeds 80 columns then it wont work. Will you also explain each line so i can get more idea of whats going on? ...
www.computing.net/answers/unix/appending-record-with-same-field/5855.html








Which MP3 player do you have?

iPod/iPhone
Zune
Something Else
None


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History






Data Recovery Software