Sponsored Content
Homework and Emergencies Homework & Coursework Questions Need help how to search for shortest line from a file Post 302926205 by junior-helper on Friday 21st of November 2014 07:14:30 PM
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.
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
LAM(1)							    BSD General Commands Manual 						    LAM(1)

NAME
lam -- laminate files SYNOPSIS
lam [-f min.max] [-s sepstring] [-t c] file ... lam [-p min.max] [-s sepstring] [-t c] file ... DESCRIPTION
The lam utility copies the named files side by side onto the standard output. The n-th input lines from the input files are considered frag- ments of the single long n-th output line into which they are assembled. The name `-' means the standard input, and may be repeated. Normally, each option affects only the file after it. If the option letter is capitalized it affects all subsequent files until it appears again uncapitalized. The options are described below: -f min.max Print line fragments according to the format string min.max, where min is the minimum field width and max the maximum field width. If min begins with a zero, zeros will be added to make up the field width, and if it begins with a `-', the fragment will be left- adjusted within the field. -p min.max Like -f, but pad this file's field when end-of-file is reached and other files are still active. -s sepstring Print sepstring before printing line fragments from the next file. This option may appear after the last file. -t c The input line terminator is c instead of a newline. The newline normally appended to each output line is omitted. To print files simultaneously for easy viewing use pr(1). EXAMPLES
The command lam file1 file2 file3 file4 joins 4 files together along each line. To merge the lines from four different files use lam file1 -S " " file2 file3 file4 Every 2 lines of a file may be joined on one line with lam - - < file and a form letter with substitutions keyed by `@' can be done with lam -t @ letter changes SEE ALSO
join(1), paste(1), pr(1), printf(3) STANDARDS
Some of the functionality of lam is standardized as the paste(1) utility by IEEE Std 1003.2 (``POSIX.2''). BUGS
The lam utility does not recognize multibyte characters. BSD
August 12, 2004 BSD
All times are GMT -4. The time now is 01:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy