Hello everyone...
I need to find out, how to find longest line or possibly lines in several files which are arguments for script. The thing is, that I tried some possibilities before, but nothing worked correctly.
Example
when i use:
awk ' { if ( length > L ) { L=length ;s=$0 } }END{ print L,"\""s"\"" }' "file"
it will only print 1 longest line. But what if there are 2 or more lines with exact length? Or there are similar lines in different files?
awk '{print length, $0}' | sort -nr | head -1
this is my second through, but same problem as above and i don't know how to make it.
Shell version maxlength=0
while IFS= read -r line
do
if [ ${#line} -eq $maxlength ]; then
p="${p}
${#line} \"$line\""
elif [ ${#line} -gt $maxlength ]; then
p="${#line} \"$line\""
maxlength=${#line}
fi
done < infile
echo "$p"
Thank for that, i really appreciate that. But now something more difficult. I have written a bash script which should find the longest line or lines from all of the files which are arguments for script. If there are no argument, standart input is selected... And script should have help. So my question is, why this my script isn't working at all?
#! /usr/local/bin/bash2
print_help ()
{
echo "The longest file or files"
echo "-h shows help"
}
riadok=""
for i in "$@"; do
case "$1" in
-h) print_help
exit 0
break
;;
*) break
;;
esac
if ["$0" -gt 0]
then
riadok="$(awk 'length==L{p=p RS L FS "\""$0"\""} length > L{L=length; p=L FS "\""$0"\""}END{ print p}' $0)"
pole=("${pole[@]}" "$riadok")
fi
shift
done
for i in "${pole[@]}"; do
echo "$i"
done
exit 0
Presuming /usr/local/bin/bash2 exists on your system, this will not work:
if ["$0" -gt 0]
there should be spaces and quotes have no business in a numerical context
if [ $0 -gt 0 ]
But seeing if $0 is greater than -0 makes no sense. Perhaps you mean
if [ $# -gt 0 ]
also: case $1 should be case $i and so you really want to break out of the for loop with a break statement, you mix an array pole with a variable assignment, etc.
I think you need to have a thorough look at your script...
Well, i am bit confused... So i make changes you was writing... Script isn't working, so you are right, i need to check it... Thanks a lot... And that array.
I need to put the the longest line or lines into the array... Well, i thought that it could be like this
I a, having a hard time trying to figure out what you are trying to do. You mention using reading standard input, but I see no read statements. Besides that would not even be possible, at least you could only determine the longest line in standard input, not in multiple files.
Why would this not work:
print_help ()
{
echo "The longest line in file or files"
echo "-h shows help"
}
for i in "$@"; do
case "$1" in
-h) print_help
exit 0
esac
done
awk 'length==L{p=p RS L FS "\""$0"\""} length > L{L=length; p=L FS "\""$0"\""}END{ print p}' "$@"
It looks like i didn't tell clearly... Sorry for that... The input could be files, or nothing and in that case i should use stdin. Then should script find the longest possible line or eventually lines. Of course, -h could be there to echo help. I rewrite some of that rubbish that there was before... Now, my scritp looks like this....
#! /usr/local/bin/bash2
print_help ()
{
echo "the longest line or lines in stdin or file or files"
echo "-h show help"
}
riadok=""
for i in "$@"; do
case "$i" in
-h) vypis_helpu
exit 0
;;
*)
riadok=$(awk 'length==L{r=r RS L FS ""$0""} length > L{L=length; r=L FS ""$0""}END{ print r}' "$i")
pole=("${pole[@]}" "$riadok")
;;
esac
shift
done
for i in "${pole[@]}"; do
echo "$i" | sort -n | tail -1
done
exit 0
Now i have a problem with set the output from awk to the variable riadok and then to put riadok into array. Hope, that this is more accurate than my last comment.
---------- Post updated at 10:16 AM ---------- Previous update was at 10:15 AM ----------
So it is "the longest line or lines in stdin or file or files"
i tried to use awk inside of for to ensure, that awk will be executed for every file, that comes in. And then, the longest line or possible lines should be written to the variable. So in the and, i should have array of strings with the longest lines from all files... And from that array i choose the longest..
---------- Post updated at 10:27 AM ---------- Previous update was at 10:24 AM ----------
it gonna be automatic test and i shouldn't use cat... And i must figure out how to patch that for stdin inside script...
The awk already does all that it chooses the longest line(s) from all files. I just used cat as an example to generate input on stdin for the shell script, you can use any process there. Inside the script the awk statement will then either read the files provided as parameters to the script or read from stdin if not file names are provided.
Well, i just finish testing... Awk is now good but it put all the lines into variable... So if i have 3 longest lines with same length, awk save all three to riadok... So it is not very good. But if awk find all longest lines from every file on input, how can i find the longest from the longest?
---------- Post updated at 11:14 AM ---------- Previous update was at 11:01 AM ----------
That code of yours isn't working, i tried to execute script ./script aaa bbb ccc...
and it didn't read stdin, just error message showed...
---------- Post updated at 11:34 AM ---------- Previous update was at 11:14 AM ----------
I need to get output from this to the variable riadok. It works, when i've got files with only one the longest line in each file. But when it is more than that in single file, it put all lines into single variable. How can i fix this? I tried these options:
Of course it didn't read stdin. You did not provide anything on stdin. You would need for example a pipe to do that, like I did with the cat example.
And what code did you use? The one in Bash script find longest line/lines in several files Post: 302466294 ? What error message did you get?
well, that error message was cant open file.... My fault... That automatic test provide som stdin and test it... But i wonder, how can i fix that awk, because if it is not possible, i must rewrite all this script and make it without awk. Probably it could be possible to do when i write a length of line to the line a sort it... I must chcecked it.
---------- Post updated at 07:48 PM ---------- Previous update was at 11:51 AM ----------
Ok, i finally get it... Well, i am really dumbass... I am sorry for all... My script is almost finished, i only must finish errors. So thank you, you saved my life...
---------- Post updated at 08:12 PM ---------- Previous update was at 07:48 PM ----------
it is in script. It will echo the number of characters from the longest line, but i need to print a number of line in file, and whole input should have a form:
Output: '<file>: <number of line> <length of line> <line>'
How can i make it without rewriting whole code?
it actually does the "length of line> <line>" But i need to force it have output in that form... Please help. I have tried to echo
"Output: '$@: '" and awk -F, '{if($2=="") print NR}'
should give numbers to lines. But how to make it at once?
It is working just nice... It now write all information i need... Except one... I need before that and after a '... How can i do that? Nearly our i tried " ' ` and nothing works... If you know how to do this, this is my last comment here...