How to find the longest line in a file using bash script? [duplicate]












0
















This question already has an answer here:




  • Longest line in a file

    13 answers




can anyone give a look to this bash script of mine please? I am trying to find the longest line in a file using a bash script so I wrote this.



#!/bin/bash

#Francesco Foschi length of a row in a file

let n=0
let max_length=0

while read row
do
length=$(echo -n $row | wc -c)
if[ ${#length} -gt ${#max_length} ]
then
let max_length=${#length}
fi

echo "$n row is $length charachters long"
echo "$row"
let n=n+1

done < $1

echo "longest line is $max_length charachters long"

exit 0


Every time I try to run the console says that I have a syntax error near the unexpected then token. What am I doing wrong??



BTW running of fedora28










share|improve this question















marked as duplicate by Robin Green, shellter, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 5:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    You should put a space after the word if.

    – Robin Green
    Nov 24 '18 at 16:00






  • 4





    I suggest you to check your code in shellcheck.net

    – ingroxd
    Nov 24 '18 at 16:22











  • length="${#row}"

    – Cyrus
    Nov 24 '18 at 17:19













  • Bash is a supremely slow and tacky tool for this particular task. You want to investigate reimplementing the script in Awk for any real-world scenario.

    – tripleee
    Nov 26 '18 at 5:56











  • I rolled back your latest edit. Questions on Stack Overflow should remain strictly questions; the way to mark something as a suitable answer is simply to accept an answer, like you already did. (The question's edit history still contains all the text if you want to recover it.)

    – tripleee
    Nov 26 '18 at 5:57
















0
















This question already has an answer here:




  • Longest line in a file

    13 answers




can anyone give a look to this bash script of mine please? I am trying to find the longest line in a file using a bash script so I wrote this.



#!/bin/bash

#Francesco Foschi length of a row in a file

let n=0
let max_length=0

while read row
do
length=$(echo -n $row | wc -c)
if[ ${#length} -gt ${#max_length} ]
then
let max_length=${#length}
fi

echo "$n row is $length charachters long"
echo "$row"
let n=n+1

done < $1

echo "longest line is $max_length charachters long"

exit 0


Every time I try to run the console says that I have a syntax error near the unexpected then token. What am I doing wrong??



BTW running of fedora28










share|improve this question















marked as duplicate by Robin Green, shellter, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 5:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    You should put a space after the word if.

    – Robin Green
    Nov 24 '18 at 16:00






  • 4





    I suggest you to check your code in shellcheck.net

    – ingroxd
    Nov 24 '18 at 16:22











  • length="${#row}"

    – Cyrus
    Nov 24 '18 at 17:19













  • Bash is a supremely slow and tacky tool for this particular task. You want to investigate reimplementing the script in Awk for any real-world scenario.

    – tripleee
    Nov 26 '18 at 5:56











  • I rolled back your latest edit. Questions on Stack Overflow should remain strictly questions; the way to mark something as a suitable answer is simply to accept an answer, like you already did. (The question's edit history still contains all the text if you want to recover it.)

    – tripleee
    Nov 26 '18 at 5:57














0












0








0









This question already has an answer here:




  • Longest line in a file

    13 answers




can anyone give a look to this bash script of mine please? I am trying to find the longest line in a file using a bash script so I wrote this.



#!/bin/bash

#Francesco Foschi length of a row in a file

let n=0
let max_length=0

while read row
do
length=$(echo -n $row | wc -c)
if[ ${#length} -gt ${#max_length} ]
then
let max_length=${#length}
fi

echo "$n row is $length charachters long"
echo "$row"
let n=n+1

done < $1

echo "longest line is $max_length charachters long"

exit 0


Every time I try to run the console says that I have a syntax error near the unexpected then token. What am I doing wrong??



BTW running of fedora28










share|improve this question

















This question already has an answer here:




  • Longest line in a file

    13 answers




can anyone give a look to this bash script of mine please? I am trying to find the longest line in a file using a bash script so I wrote this.



#!/bin/bash

#Francesco Foschi length of a row in a file

let n=0
let max_length=0

while read row
do
length=$(echo -n $row | wc -c)
if[ ${#length} -gt ${#max_length} ]
then
let max_length=${#length}
fi

echo "$n row is $length charachters long"
echo "$row"
let n=n+1

done < $1

echo "longest line is $max_length charachters long"

exit 0


Every time I try to run the console says that I have a syntax error near the unexpected then token. What am I doing wrong??



BTW running of fedora28





This question already has an answer here:




  • Longest line in a file

    13 answers








bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 5:56









tripleee

93.4k13130184




93.4k13130184










asked Nov 24 '18 at 15:56









francesco foschifrancesco foschi

186




186




marked as duplicate by Robin Green, shellter, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 5:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Robin Green, shellter, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 5:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    You should put a space after the word if.

    – Robin Green
    Nov 24 '18 at 16:00






  • 4





    I suggest you to check your code in shellcheck.net

    – ingroxd
    Nov 24 '18 at 16:22











  • length="${#row}"

    – Cyrus
    Nov 24 '18 at 17:19













  • Bash is a supremely slow and tacky tool for this particular task. You want to investigate reimplementing the script in Awk for any real-world scenario.

    – tripleee
    Nov 26 '18 at 5:56











  • I rolled back your latest edit. Questions on Stack Overflow should remain strictly questions; the way to mark something as a suitable answer is simply to accept an answer, like you already did. (The question's edit history still contains all the text if you want to recover it.)

    – tripleee
    Nov 26 '18 at 5:57














  • 2





    You should put a space after the word if.

    – Robin Green
    Nov 24 '18 at 16:00






  • 4





    I suggest you to check your code in shellcheck.net

    – ingroxd
    Nov 24 '18 at 16:22











  • length="${#row}"

    – Cyrus
    Nov 24 '18 at 17:19













  • Bash is a supremely slow and tacky tool for this particular task. You want to investigate reimplementing the script in Awk for any real-world scenario.

    – tripleee
    Nov 26 '18 at 5:56











  • I rolled back your latest edit. Questions on Stack Overflow should remain strictly questions; the way to mark something as a suitable answer is simply to accept an answer, like you already did. (The question's edit history still contains all the text if you want to recover it.)

    – tripleee
    Nov 26 '18 at 5:57








2




2





You should put a space after the word if.

– Robin Green
Nov 24 '18 at 16:00





You should put a space after the word if.

– Robin Green
Nov 24 '18 at 16:00




4




4





I suggest you to check your code in shellcheck.net

– ingroxd
Nov 24 '18 at 16:22





I suggest you to check your code in shellcheck.net

– ingroxd
Nov 24 '18 at 16:22













length="${#row}"

– Cyrus
Nov 24 '18 at 17:19







length="${#row}"

– Cyrus
Nov 24 '18 at 17:19















Bash is a supremely slow and tacky tool for this particular task. You want to investigate reimplementing the script in Awk for any real-world scenario.

– tripleee
Nov 26 '18 at 5:56





Bash is a supremely slow and tacky tool for this particular task. You want to investigate reimplementing the script in Awk for any real-world scenario.

– tripleee
Nov 26 '18 at 5:56













I rolled back your latest edit. Questions on Stack Overflow should remain strictly questions; the way to mark something as a suitable answer is simply to accept an answer, like you already did. (The question's edit history still contains all the text if you want to recover it.)

– tripleee
Nov 26 '18 at 5:57





I rolled back your latest edit. Questions on Stack Overflow should remain strictly questions; the way to mark something as a suitable answer is simply to accept an answer, like you already did. (The question's edit history still contains all the text if you want to recover it.)

– tripleee
Nov 26 '18 at 5:57












3 Answers
3






active

oldest

votes


















2














try this:



#!/bin/bash
#Francesco Foschi length of a row in a file

let n=0
let max_length=0

while read row
do
length=$(echo -n $row | wc -c)
if [ ${length} -gt ${max_length} ]
then
let max_length=${length}
fi

echo "$n row is $length charachters long"
echo "$row"
let n=n+1

done < $1

echo "longest line is $max_length charachters long"

exit 0





share|improve this answer































    2














    GNU wc has this functionality built in:



    -L, --max-line-length
    print the maximum display width





    share|improve this answer
























    • I knew this but I am trying to go full POSIX

      – francesco foschi
      Nov 24 '18 at 17:00



















    1














    plain bash



    #!/bin/bash
    max=-1
    while IFS= read -r line; do
    [[ ${#line} -gt $max ]] && max=${#line}
    done < "$1"
    echo "longest line is $max chars long"




    This idiom is used to read the line exactly verbatim: IFS= read -r line



    Demo:





    1. create a file with leading/trailing whitespace and a backslash



      $ echo ' hbHello ' > file


      this file is 10 bytes in size (not counting the trailing newline).




    2. read it with plain read var



      $ read line < file; printf %s "$line" | od -c
      0000000 h b H e l l o
      0000007


      Only 7 chars: missing the backslash and the whitespace




    3. add the -r option for read:



      $ read -r line < file; printf %s "$line" | od -c
      0000000 h b H e l l o
      0000010


      Now we have 8 characters (the "0000010" is octal), but still missing the whitespace.




    4. add the IFS= variable assignment:



      $ IFS= read -r line < file; printf %s "$line" | od -c
      0000000 h b H e l l o
      0000012


      10 characters (octal 12): now we have exactly what was written to the file in $line.




    It's a pain to have to write IFS= read -r line all the time, but bash inflicts considerable pain on the programmer.






    share|improve this answer






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      try this:



      #!/bin/bash
      #Francesco Foschi length of a row in a file

      let n=0
      let max_length=0

      while read row
      do
      length=$(echo -n $row | wc -c)
      if [ ${length} -gt ${max_length} ]
      then
      let max_length=${length}
      fi

      echo "$n row is $length charachters long"
      echo "$row"
      let n=n+1

      done < $1

      echo "longest line is $max_length charachters long"

      exit 0





      share|improve this answer




























        2














        try this:



        #!/bin/bash
        #Francesco Foschi length of a row in a file

        let n=0
        let max_length=0

        while read row
        do
        length=$(echo -n $row | wc -c)
        if [ ${length} -gt ${max_length} ]
        then
        let max_length=${length}
        fi

        echo "$n row is $length charachters long"
        echo "$row"
        let n=n+1

        done < $1

        echo "longest line is $max_length charachters long"

        exit 0





        share|improve this answer


























          2












          2








          2







          try this:



          #!/bin/bash
          #Francesco Foschi length of a row in a file

          let n=0
          let max_length=0

          while read row
          do
          length=$(echo -n $row | wc -c)
          if [ ${length} -gt ${max_length} ]
          then
          let max_length=${length}
          fi

          echo "$n row is $length charachters long"
          echo "$row"
          let n=n+1

          done < $1

          echo "longest line is $max_length charachters long"

          exit 0





          share|improve this answer













          try this:



          #!/bin/bash
          #Francesco Foschi length of a row in a file

          let n=0
          let max_length=0

          while read row
          do
          length=$(echo -n $row | wc -c)
          if [ ${length} -gt ${max_length} ]
          then
          let max_length=${length}
          fi

          echo "$n row is $length charachters long"
          echo "$row"
          let n=n+1

          done < $1

          echo "longest line is $max_length charachters long"

          exit 0






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 16:35









          gopygopy

          1687




          1687

























              2














              GNU wc has this functionality built in:



              -L, --max-line-length
              print the maximum display width





              share|improve this answer
























              • I knew this but I am trying to go full POSIX

                – francesco foschi
                Nov 24 '18 at 17:00
















              2














              GNU wc has this functionality built in:



              -L, --max-line-length
              print the maximum display width





              share|improve this answer
























              • I knew this but I am trying to go full POSIX

                – francesco foschi
                Nov 24 '18 at 17:00














              2












              2








              2







              GNU wc has this functionality built in:



              -L, --max-line-length
              print the maximum display width





              share|improve this answer













              GNU wc has this functionality built in:



              -L, --max-line-length
              print the maximum display width






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 24 '18 at 16:46









              codycody

              6,77131125




              6,77131125













              • I knew this but I am trying to go full POSIX

                – francesco foschi
                Nov 24 '18 at 17:00



















              • I knew this but I am trying to go full POSIX

                – francesco foschi
                Nov 24 '18 at 17:00

















              I knew this but I am trying to go full POSIX

              – francesco foschi
              Nov 24 '18 at 17:00





              I knew this but I am trying to go full POSIX

              – francesco foschi
              Nov 24 '18 at 17:00











              1














              plain bash



              #!/bin/bash
              max=-1
              while IFS= read -r line; do
              [[ ${#line} -gt $max ]] && max=${#line}
              done < "$1"
              echo "longest line is $max chars long"




              This idiom is used to read the line exactly verbatim: IFS= read -r line



              Demo:





              1. create a file with leading/trailing whitespace and a backslash



                $ echo ' hbHello ' > file


                this file is 10 bytes in size (not counting the trailing newline).




              2. read it with plain read var



                $ read line < file; printf %s "$line" | od -c
                0000000 h b H e l l o
                0000007


                Only 7 chars: missing the backslash and the whitespace




              3. add the -r option for read:



                $ read -r line < file; printf %s "$line" | od -c
                0000000 h b H e l l o
                0000010


                Now we have 8 characters (the "0000010" is octal), but still missing the whitespace.




              4. add the IFS= variable assignment:



                $ IFS= read -r line < file; printf %s "$line" | od -c
                0000000 h b H e l l o
                0000012


                10 characters (octal 12): now we have exactly what was written to the file in $line.




              It's a pain to have to write IFS= read -r line all the time, but bash inflicts considerable pain on the programmer.






              share|improve this answer




























                1














                plain bash



                #!/bin/bash
                max=-1
                while IFS= read -r line; do
                [[ ${#line} -gt $max ]] && max=${#line}
                done < "$1"
                echo "longest line is $max chars long"




                This idiom is used to read the line exactly verbatim: IFS= read -r line



                Demo:





                1. create a file with leading/trailing whitespace and a backslash



                  $ echo ' hbHello ' > file


                  this file is 10 bytes in size (not counting the trailing newline).




                2. read it with plain read var



                  $ read line < file; printf %s "$line" | od -c
                  0000000 h b H e l l o
                  0000007


                  Only 7 chars: missing the backslash and the whitespace




                3. add the -r option for read:



                  $ read -r line < file; printf %s "$line" | od -c
                  0000000 h b H e l l o
                  0000010


                  Now we have 8 characters (the "0000010" is octal), but still missing the whitespace.




                4. add the IFS= variable assignment:



                  $ IFS= read -r line < file; printf %s "$line" | od -c
                  0000000 h b H e l l o
                  0000012


                  10 characters (octal 12): now we have exactly what was written to the file in $line.




                It's a pain to have to write IFS= read -r line all the time, but bash inflicts considerable pain on the programmer.






                share|improve this answer


























                  1












                  1








                  1







                  plain bash



                  #!/bin/bash
                  max=-1
                  while IFS= read -r line; do
                  [[ ${#line} -gt $max ]] && max=${#line}
                  done < "$1"
                  echo "longest line is $max chars long"




                  This idiom is used to read the line exactly verbatim: IFS= read -r line



                  Demo:





                  1. create a file with leading/trailing whitespace and a backslash



                    $ echo ' hbHello ' > file


                    this file is 10 bytes in size (not counting the trailing newline).




                  2. read it with plain read var



                    $ read line < file; printf %s "$line" | od -c
                    0000000 h b H e l l o
                    0000007


                    Only 7 chars: missing the backslash and the whitespace




                  3. add the -r option for read:



                    $ read -r line < file; printf %s "$line" | od -c
                    0000000 h b H e l l o
                    0000010


                    Now we have 8 characters (the "0000010" is octal), but still missing the whitespace.




                  4. add the IFS= variable assignment:



                    $ IFS= read -r line < file; printf %s "$line" | od -c
                    0000000 h b H e l l o
                    0000012


                    10 characters (octal 12): now we have exactly what was written to the file in $line.




                  It's a pain to have to write IFS= read -r line all the time, but bash inflicts considerable pain on the programmer.






                  share|improve this answer













                  plain bash



                  #!/bin/bash
                  max=-1
                  while IFS= read -r line; do
                  [[ ${#line} -gt $max ]] && max=${#line}
                  done < "$1"
                  echo "longest line is $max chars long"




                  This idiom is used to read the line exactly verbatim: IFS= read -r line



                  Demo:





                  1. create a file with leading/trailing whitespace and a backslash



                    $ echo ' hbHello ' > file


                    this file is 10 bytes in size (not counting the trailing newline).




                  2. read it with plain read var



                    $ read line < file; printf %s "$line" | od -c
                    0000000 h b H e l l o
                    0000007


                    Only 7 chars: missing the backslash and the whitespace




                  3. add the -r option for read:



                    $ read -r line < file; printf %s "$line" | od -c
                    0000000 h b H e l l o
                    0000010


                    Now we have 8 characters (the "0000010" is octal), but still missing the whitespace.




                  4. add the IFS= variable assignment:



                    $ IFS= read -r line < file; printf %s "$line" | od -c
                    0000000 h b H e l l o
                    0000012


                    10 characters (octal 12): now we have exactly what was written to the file in $line.




                  It's a pain to have to write IFS= read -r line all the time, but bash inflicts considerable pain on the programmer.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 17:28









                  glenn jackmanglenn jackman

                  169k26147239




                  169k26147239















                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin