LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2011, 12:43 AM   #1
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Rep: Reputation: Disabled
Help with bash script


Code:
#!/bin/bash 
echo text file name  
read x
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 < "$x"
echo "$p"
This script shows longest line. How i can make a script which shows number of longest line

Last edited by trintukaz; 10-02-2011 at 03:23 AM.
 
Old 10-02-2011, 04:08 AM   #2
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
any ideas?
 
0 members found this post helpful.
Old 10-02-2011, 04:12 AM   #3
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
if I understand the question right, just remember the line number

Code:
#!/bin/bash 
echo text file name  
read x
maxlength=0
linenr=0

while IFS= read -r line
do
let "linenr += 1"
if [ ${#line} -eq $maxlength ]; then
p="${p}
${#line} \"$line\""
elif [ ${#line} -gt $maxlength ]; then
p="${#line} \"$line\" in line ${linenr}"
maxlength=${#line}
fi
done < "$x"
echo "$p"
 
1 members found this post helpful.
Old 10-02-2011, 04:45 AM   #4
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
You understood right but I need just number of line without text
 
Old 10-02-2011, 04:54 AM   #5
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
just remove all unwanted


#!/bin/bash
echo text file name
read x
maxlength=0
linenr=0

while IFS= read -r line
do
let "linenr += 1"
if [ ${#line} -eq $maxlength ]; then
p="${p}
${#line} \"$line\""
elif [ ${#line} -gt $maxlength ]; then
p=${linenr}
maxlength=${#line}
fi
done < "$x"
echo "$p"
 
1 members found this post helpful.
Old 10-02-2011, 05:11 AM   #6
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Thank you. There is a problem if text file have few longest lines
output
Quote:
1
3 "669"
3 "459"
text file
Quote:
456
669

459
 
Old 10-02-2011, 05:19 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Your if requests that it gets printed. Maybe you need to explain better what you are looking for?
 
Old 10-02-2011, 05:25 AM   #8
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
i need only line number without text. First line written good but rest of lines shows text and not that number where the longest line is. Hope you understand.
 
Old 10-02-2011, 05:39 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well assuming I understand:
Code:
#!/bin/bash

l=m=0

while read -r line
do
    ((l++))

    if (( ${#line} > m ))
    then
        m=${#line}
        p=$l
    fi
done<file

echo $p
 
Old 10-02-2011, 05:49 AM   #10
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Text file have 3 equal lines, this script shows only one. I need all.
 
0 members found this post helpful.
Old 10-02-2011, 06:01 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So what would be the output with your previous example?
 
Old 10-02-2011, 06:10 AM   #12
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
For example text file
Quote:
blah
blah
blah

blah
Output should be
Quote:
longest lines
1
2
3
5
 
Old 10-02-2011, 06:35 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Right ... so then something like:
Code:
#!/bin/bash

l=0

while read -r line
do
    ((l++))
    [[ ${a[${#line}]} ]] && a[${#line}]+=" $l" || a[${#line}]=$l
done<$1

echo ${a[@]:(-1)}
 
1 members found this post helpful.
Old 10-02-2011, 06:43 AM   #14
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Thank you very much for your pation.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
Bash:Printing the line number in bash script suryaemlinux Programming 2 02-05-2011 09:59 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration