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
# 1  
Old 11-18-2014
Need help how to search for shortest line from a file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
I have to write a program that have to read every standard input then print out the line number and the content of longest line and shortest line.

So, far I have figured out the to find the longest line from the input file and still searching for the hints of what command to use for finding the shortest line.

Would someone would give me a hint for that?

Thanks in advance,

Scopiop

2. Relevant commands, code, scripts, algorithms:

Code:
cat -n inputfile

     1  The combination of bold design.
     2  Other on-board perks include a 20% increase in capacity per train.
     3  The new design was

output
Code:
2:Other on-board perks include a 20% increase in capacity per train.

3. The attempts at a solution (include all code and scripts):
Code:
egrep -n "^.{$(wc -L < inputfile)}$" inputfile

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

City College of San Francisco/Aaron Brick/160B
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 11-18-2014
As we are not allowed to provide full solutions in the homework sub-forum, I suggest following code (you can build upon):
Code:
linenumber=1

while read line; do
 echo "line length" "line number" "$line" # hint for line length: ${#parameter}
 # increment line number by one here, hint: $((expression))
done < inputfile > outputfile # everything you echoed above goes to the outputfile

1. Go through the inputfile line by line in a while loop
2. Print line length, line number and line
3. Afterwards you get the longest and shortest line by utilizing (hint: using pipes) following tools on the outputfile:
sort -n, head/tail -n1 and cut -d' ' -f2- (check out the man pages of the particular tools to learn the meaning of the options I mentioned for the particular tool)

Hope this helps.
# 3  
Old 11-18-2014
I'm pretty sure the output you have given is NOT the result of the code you have presented.

What tools (e.g. awk) are allowed? It can be done with only shell (bash) builtins (while loop, read, parameter expansion, arithmetic evaluation).

EDIT: and junior-helper has given you hands-on examples for above...
# 4  
Old 11-19-2014
Thank you Junior-helper and RudiC.
# 5  
Old 11-21-2014
I have ran in the problem since the instructor does not allow me to send my output to another file and read back in again. He prefers that the program will output max and min lines without using a temp file. Therefore, I rewrote my program by using many if elif and else statement to filter out all lines as possible to find the max and min lines. But I apparently ran into a problem. If the blank line has a space, which will be my shortest line is 1 character but display is nothing. There fore I want to filter out the blank space which is equivalent as a blank line. To set a space in one of my elif statement, how do I do that? I have tried
Code:
 
elif ((${#line} != '  ' )) 
#also 
elif (($#line} != " " ))

it was unsuccessful to resolve my problem
inputfile
Code:
Why first
 
hello
 
I don't have any idea

now what it is
I am very sure the output out have given is NOT the result of the code you have presented.

output
Code:
The longest line is 90 characters long line# 8: I am very sure the output out have given is NOT the result of the code you have presented.

The shortest line is 1 characters long line# 2:

Thanks in advance,

Scopiop
# 6  
Old 11-21-2014
${#line} will hold the line lenght, eg. 90. That is the reason why ${#line} != ' ' is not going to work.

HINT: If the line is a "real" blank line (no spaces at all) or is a blank line *with* spaces and/or tabs only, ${#line} will always be 0.
Code:
# filtering out "real" blank lines (without spaces and/or tabs)
# AND/OR blank lines with spaces and/or tabs
if [ ${#line} -ne 0 ]
 then
 # perform your checks here only on lines with length != 0 (non-blank lines).
fi

Does it help you further?
# 7  
Old 11-21-2014
I have tried to test the input file that has 1 blank includes with 2 characters SPACE and here the result.
Code:
 
if [[ ${#line} > $maxline && ${#line} != 0 ]]


input
Code:
            <-- it's blank here with 2 SPACE characters

output
Code:
 Standard input only have one line therefore only one line output

The longest line is 2 characters long line# 1:

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