Need help how to search for shortest line from a file


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Need help how to search for shortest line from a file
# 8  
Old 11-21-2014
Quote:
Originally Posted by scopiop
Code:
if [[ ${#line} > $maxline && ${#line} != 0 ]]

That seems not to produce the expected output Smilie
Code:
$ while read line; do if [[ ${#line} > 10 && ${#line} != 0 ]]; then echo ${#line}; fi; done <inputfile 
9
5
21
14
90

Try
Code:
if [[ ${#line} -gt $maxline && ${#line} != 0 ]]

# 9  
Old 11-21-2014
you can see my attachfile to run my codes

Here is how the instructor want us run the program

$ cat inputfile | program.sh

Please create an inputfile that contains a blank line with a few SPACE characters in it. You will see what I mean.

Thank you,

Scopiop

Last edited by scopiop; 11-21-2014 at 10:12 PM..
# 10  
Old 11-21-2014
Code:
./linelengthhw2vs2.sh: line 34: -r: command not found

It should be while IFS= read -r line
If you reset IFS (IFS=), then all I said before, namely ${#line} is always 0 on blank lines, is not valid anymore!!
With IFS= the leading spaces are preserved and thus counted.

If you want to keep IFS= then put following immediately after the do command: line=${line## *}
That will delete all leading spaces.

When you change that, your script should perform much better:
Code:
$ cat testfile
123
      <- here are 5 spaces
abcde
aaaaaaaaa
   <- here are 2 spaces
aaaaaa
$ cat testfile | ./linelength.sh 
line either blank or between max or min
line either blank or between max or min
The longest line is 9 characters line:  4 contents aaaaaaaaa
The shortest line is 6 characters line:  6 contents aaaaaa
$

You see, the blank lines are properly recognized, but there is another "bug" in the report of the shortest line. The shortest line is line 1.

I enabled the debugging option in the script set -x
Code:
      -x  Print commands and their arguments as they are executed

Code:
$ cat testfile | ./linelength.sh 
+ linecount=1
+ maxlinechars=0
+ minlinechars=0
+ maxcontents=0
+ mincontents=0
+ maxlinenumber=0
+ minlinenumber=0
+ IFS=
+ read -r line
+ line=123
+ ((  3 > 0  ))
+ maxlinechars=3           #
+ maxcontents=123          # OK, you stored the values for the first string as longest string. So far, so good.
+ maxlinenumber=1          #
+ (( linecount++ ))
+ IFS=
+ read -r line
+ line=
+ ((  0 > 3  ))
+ ((  0 == 0 && 0 != 0  ))
+ ((  0 < 0 && 0 != 0  ))
+ echo line either blank or between max or min
line either blank or between max or min
+ (( linecount++ ))
+ IFS=
+ read -r line
+ line=abcde
+ ((  5 > 3  ))
+ maxlinechars=5           # Next line is longer than the previous one...
+ maxcontents=abcde        # At this moment, line number 1 has the shortest string, but that's somehow ignored here
+ maxlinenumber=3          # and leads to an incorrect report at the end.
+ (( linecount++ ))
...

Hope this helps.
# 11  
Old 11-21-2014
Thanks, I remove the IFS and it solved all my problem. I did not need to use FIELD SEPARATOR in this case of reading.... Thank you much.

scopiop
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search words in multiple file line by line

Hi All I have to search servers name say like 1000+ "unique names" line by line in child.txt files in another file that is a master file where all server present say "master.txt",if child.txt's server name matches with master files then it print yes else no with server name. (4 Replies)
Discussion started by: netdbaind
4 Replies

2. Shell Programming and Scripting

Shortest path for each query from a csv file

Hi all, I have this file __DATA__ child, Parent, probability, M7, Q, P, M7, M28, E, M28, M6, E, M6, Q, Pl, & several hundred lines..... Legends: P(= Probable) > Pl(=Plausible) > E(=Equivocal). What I want is for each child I want to trace it... (5 Replies)
Discussion started by: rushadrena
5 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

How to select the shortest path in grep search?

Hi, How can I display only one shortest path (A/B/configure)? $ grep configure file.txt A/B/configure A/B/C/configure A/B/C/D/configure Thank you. (9 Replies)
Discussion started by: hce
9 Replies

5. Shell Programming and Scripting

how to find the shortest line which containing a key string?

hi all, suppose a key string: M0271857 and to find all lines containing this key string in a text file which returns multiple lines but i only want the shortest one is there a way to do that? thanks so much! (4 Replies)
Discussion started by: sunnydanniel
4 Replies

6. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

7. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

8. Shell Programming and Scripting

Optimised way for search & replace a value on one line in a very huge file (File Size is 24 GB).

Hi Experts, I had to edit (a particular value) in header line of a very huge file so for that i wanted to search & replace a particular value on a file which was of 24 GB in Size. I managed to do it but it took long time to complete. Can anyone please tell me how can we do it in a optimised... (7 Replies)
Discussion started by: manishkomar007
7 Replies

9. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

10. Shell Programming and Scripting

Search for a whole line in a file

Hi All, Please can anyone advise how can I search a whole line in a file. Best Regards, Shazin (1 Reply)
Discussion started by: Shazin
1 Replies
Login or Register to Ask a Question