Bash 如何计算字符串第一行的字符数?

Bash 如何计算字符串第一行的字符数?,bash,Bash,我知道如何在bash中计算字符串的长度: var1=$'Title\n\nsome text some text some text some text' echo "$var1" length=${#var1} echo "Length: $length" 我还知道如何计算文件每行中的字符数: awk '{ print length($0); }' file 但它不适用于bash变量,仅适用于文件: awk: fatal: cannot open file `var1' for readi

我知道如何在bash中计算字符串的长度:

var1=$'Title\n\nsome text some text some text some text'
echo "$var1"
length=${#var1}
echo "Length: $length"
我还知道如何计算文件每行中的字符数:

awk '{ print length($0); }' file
但它不适用于bash变量,仅适用于文件:

awk: fatal: cannot open file `var1' for reading (No such file or directory)
但我想计算第1行和第3行(或特定行)中的字符数。不是所有的长度,因为字符串可能很长。可能吗

var1=$'Title\n\nsome text some text some text some text'
echo "$var1"
length_of_1st_line=[???]
length_of_3rd_line=[???]
echo "$length_of_1st_line"
echo "$length_of_3rd_line"

如何计算bash中字符串变量的第一行(或第三行,或任何其他给定行)的字符数并将其保存在变量中?

您可以使用
awk
将字符串作为输入,方法是:

$>var1='Title\n\n一些文本一些文本一些文本'
$>awk“{打印长度($0)}”
输出:

5 39 5. 39 好吧,引用正确的话(:

如何计算bash中字符串变量的第一行(或第三行,或任何其他给定行)的字符数并将其保存在变量中

示例1:(1)将输入行存储在字符串中而不是文件中;(2)使用
sed
提取给定字符串的给定行,然后计算该行的长度的函数;(3)将结果存储在数组中
\u len

#!/bin/bash

# lines are stored in a string, not in a file

_string='title
this is the second line
the third line is like this
and this is the forth line, a bit longer
this one shorter'

# function to print length of line $1 of string $2

_lenln() {
  local s=$(sed -n "$1{p;q}" <<<"$2")
  echo ${#s}
}

# test that function, store results in an array _len

_len=()

_len[1]=`_lenln 1 "$_string"`
_len[2]=`_lenln 2 "$_string"`
_len[4]=`_lenln 4 "$_string"`

echo ${_len[1]}
echo ${_len[2]}
echo ${_len[4]}
#!/bin/bash

_string='title
this is the second line
the third line is like this
and this is the forth line, a bit longer
this one shorter'

_len=()

# awk to contruct bash command that calulate length of line 1 and 3
# then store those results in an array _len

eval $(awk 'NR==1||NR==3{print "_len[" NR "]=" length($0)}' <<<"$_string")

echo ${_len[1]}
echo ${_len[3]}

没有必要为此使用bash外部的任何东西(比如awk)

$ var1=$'Title\n\nsome text some text some text some text'
$ n=0; while read x; do a[$((++n))]=${#x}; done <<<"$var1"

你可能会考虑根据你的输入使用<代码> Read -Re/Cord>。

A<代码>这里的字符串让你进入其中的一部分,即“代码> AWK”{打印长度(0美元)};'但我只需要特定行的长度,而不是所有行的长度,因为字符串可能非常长。是否可以更改代码以打印给定行的长度?好的,我有:
length_of u 1st_line=$(echo-e“$var1”| awk'NR==1{print length($0);}
。或者使用sed:
length_of u of u 1st_line=$(echo-e“$var1”| sed'1!d'))
awk'NR==1 | | NR==3{print length($0);}请更详细地说明它是如何工作的。我在两个不同的系统中使用了3种不同的awk进行了测试(不过都是基于Linux的)。当我写评论时,你的答案中只有代码的第一部分,它打印的是字符串,而不是长度。在我的评论后,你添加了第二部分,但在我的控制台中显示的是“46”(是整个字符串的长度),而不是“5”。即使我将“-1”更改为“-3”它不会打印第三行的长度,所以它不是我问题的答案。那么,你的系统是什么?还有,我错过了第三行的请求,很抱歉。这个答案不适合你。Debian,还有你的?Debian和Ubuntu。好吧,@Cyrus'解决方案是正确的方式,你应该使用那个。这是对你的双引号测试我自己
$ awk "BEGIN{print length(\"$(head -1 <(echo "$var1"))\")}"
5
#!/bin/bash

# lines are stored in a string, not in a file

_string='title
this is the second line
the third line is like this
and this is the forth line, a bit longer
this one shorter'

# function to print length of line $1 of string $2

_lenln() {
  local s=$(sed -n "$1{p;q}" <<<"$2")
  echo ${#s}
}

# test that function, store results in an array _len

_len=()

_len[1]=`_lenln 1 "$_string"`
_len[2]=`_lenln 2 "$_string"`
_len[4]=`_lenln 4 "$_string"`

echo ${_len[1]}
echo ${_len[2]}
echo ${_len[4]}
#!/bin/bash

_string='title
this is the second line
the third line is like this
and this is the forth line, a bit longer
this one shorter'

_len=()

# awk to contruct bash command that calulate length of line 1 and 3
# then store those results in an array _len

eval $(awk 'NR==1||NR==3{print "_len[" NR "]=" length($0)}' <<<"$_string")

echo ${_len[1]}
echo ${_len[3]}
$ var1=$'Title\n\nsome text some text some text some text'
$ n=0; while read x; do a[$((++n))]=${#x}; done <<<"$var1"
$ declare -p a
declare -a a=([1]="5" [2]="0" [3]="39")
$ for (( n=1; n<=${#a[@]}; n++ )); do printf '%d: %d\n' "$n" "${a[$n]}"; done
1: 5
2: 0
3: 39
$ echo "${a[3]}"
39