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

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.

Seraching for records with length

Reply to Message Icon

Name: anilcgowda
Date: September 1, 2005 at 11:31:16 Pacific
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



Sponsored Link
Ads by Google

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


0

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


0

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


0

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Expression Matching Awk Variables



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: Seraching for records with length

appending records with sub category www.computing.net/answers/unix/appending-records-with-sub-category/6423.html

awk script for search and replace www.computing.net/answers/unix/awk-script-for-search-and-replace/7039.html

Appending record with same field www.computing.net/answers/unix/appending-record-with-same-field/5855.html