good friends days
I would love to know if I can help you know the length of a string
example:
cadena= "cual es mi largo"
echo "cadena : $cadena#
cadena :16
good friends days
I would love to know if I can help you know the length of a string
example:
cadena= "cual es mi largo"
echo "cadena : $cadena#
cadena :16
Hello tricampeon81,
Could you please try this and let me know if this helps you.
cadena="cual es mi largo"
echo $cadena | awk '{print length($0)}'
OR
echo "cual es mi largo" | awk '{print length($0)}'
Output will be 16
in both above codes.
Thanks,
R. Singh
How about bash?
cadena="cual es mi largo"
echo "cadena : ${#cadena}"
-Ranga
and keep the output in a variable and then make treatment
cadlen=${#cadena}
What do you mean by treatment?
I mean a long time I obtained a variable to save after doing validations that value
---------- Post updated at 02:06 PM ---------- Previous update was at 10:34 AM ----------
the truth is that it is for a log file for each nesecito long
example
file.txt
cadena1
cadenados
cadenatercera
ouput
7
9
13
I have this code does not work me
while read linea
do
#echo "Archivos Entrada: $linea"
largo=`awk '{print length($linea)}'`
echo "largo : $largo "
if [ $largo -ne 171 ]; then
echo "a es igual a 1"
fi
done < ${PATH_DAT}/$ArchPro #while read linea
---------- Post updated at 02:47 PM ---------- Previous update was at 02:06 PM ----------
and solve the problem here is the solution thank you very much to all !!
while read linea
do
#echo "Archivos Entrada: $linea"
largo=${#linea}
echo "largo es: $largo "
if [ $largo -ne 171 ]
then
echo "Registro menor a 171"
echo " $linea > ERROR DE LONGITUD" > $PATH_DAT/$ArchSalida
fi
done < ${PATH_DAT}/$ArchPro #while read line
Do you want to find the length of each line in a file?
Here's a complete example with a few methods to find the length:
$
$ cat data.log
cadena1
cadenados
cadenatercera
cadenatercera
beer
$
$ cat line_length.sh
#!/bin/bash
FILE="data.log"
while read linea
do
largo=$(echo $linea | awk '{print length($0)}') # One way to find length
len=${#linea} # And another way to find length
len1=$(awk -v L=$linea 'BEGIN{print length(L)}') # And another way to find length
len2=$(expr length $linea) # And another way to find length
len3=$(echo -n $linea | wc -c) # And another way to find length
echo $largo,$len,$len1,$len2,$len3
done < $FILE
$
$ . line_length.sh
7,7,7,7,7
9,9,9,9,9
13,13,13,13,13
13,13,13,13,13
4,4,4,4,4
$
$
The title of the post suggests that maybe you want to find the maximum length of a line in a file.
Do you want to find out the length of the longest line in your file?
In that case, a complete example is posted below:
$
$ cat data.log
cadena1
cadenados
cadenatercera
cadenatercera
beer
$
$ cat max_line_length.sh
#!/bin/bash
MAX=0
FILE="data.log"
while read linea
do
len=${#linea}
if [ $len -gt $MAX ]
then
MAX=$len
fi
done < $FILE
echo "Max length is $MAX"
$
$ . max_line_length.sh
Max length is 13
$
$
What is wrong with wc
or am I missing something?
Longhand using OSX 10.7.5, default bash terminal...
Last login: Tue Dec 22 20:16:44 on ttys000
AMIGA:barrywalker~> cadena="���� This is a � string... "
AMIGA:barrywalker~> str_length=$(wc -m <<< $cadena)
AMIGA:barrywalker~> echo "String length ="$str_length"..."
String length = 33...
AMIGA:barrywalker~> byte_length=$(wc -c <<< $cadena)
AMIGA:barrywalker~> echo "Byte length ="$byte_length"..."
Byte length = 49...
AMIGA:barrywalker~> _