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
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.
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
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 ...
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...
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? ...