How to find the longest line in a file using bash script? [duplicate]
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
bash
marked as duplicate by Robin Green, shellter, tripleee
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.
|
show 1 more comment
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
bash
marked as duplicate by Robin Green, shellter, tripleee
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 wordif
.
– 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
|
show 1 more comment
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
bash
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
bash
edited Nov 26 '18 at 5:56
tripleee
93.5k13130184
93.5k13130184
asked Nov 24 '18 at 15:56
francesco foschifrancesco foschi
186
186
marked as duplicate by Robin Green, shellter, tripleee
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
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 wordif
.
– 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
|
show 1 more comment
2
You should put a space after the wordif
.
– 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
|
show 1 more comment
3 Answers
3
active
oldest
votes
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
add a comment |
GNU wc
has this functionality built in:
-L, --max-line-length
print the maximum display width
I knew this but I am trying to go full POSIX
– francesco foschi
Nov 24 '18 at 17:00
add a comment |
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:
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).
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
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.
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.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Nov 24 '18 at 16:35
gopygopy
1687
1687
add a comment |
add a comment |
GNU wc
has this functionality built in:
-L, --max-line-length
print the maximum display width
I knew this but I am trying to go full POSIX
– francesco foschi
Nov 24 '18 at 17:00
add a comment |
GNU wc
has this functionality built in:
-L, --max-line-length
print the maximum display width
I knew this but I am trying to go full POSIX
– francesco foschi
Nov 24 '18 at 17:00
add a comment |
GNU wc
has this functionality built in:
-L, --max-line-length
print the maximum display width
GNU wc
has this functionality built in:
-L, --max-line-length
print the maximum display width
answered Nov 24 '18 at 16:46
codycody
6,78131125
6,78131125
I knew this but I am trying to go full POSIX
– francesco foschi
Nov 24 '18 at 17:00
add a comment |
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
add a comment |
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:
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).
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
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.
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.
add a comment |
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:
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).
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
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.
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.
add a comment |
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:
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).
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
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.
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.
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:
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).
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
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.
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.
answered Nov 24 '18 at 17:28
glenn jackmanglenn jackman
169k26147239
169k26147239
add a comment |
add a comment |
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