Linux 用于计算节中的行数的脚本,不包括
我正在寻找想法,让脚本执行以下操作: 我有一个文本文件,例如test.txt,其中包含类似的文本:Linux 用于计算节中的行数的脚本,不包括,linux,bash,Linux,Bash,我正在寻找想法,让脚本执行以下操作: 我有一个文本文件,例如test.txt,其中包含类似的文本: # # sectione 1 # several text lines and so on # # sectione 2 # more text and more # and more # ... # # sectione 3 # more more more # # sectione 4 # ... ... 我需要一个可能性,只计算第E 2节中的行,并排除以开始的行 在我上面的示例中,脚本
#
# sectione 1
#
several
text
lines
and so on
#
# sectione 2
#
more text
and more
#
and more
# ...
#
# sectione 3
#
more
more
more
#
# sectione 4
#
...
...
我需要一个可能性,只计算第E 2节中的行,并排除以开始的行
在我上面的示例中,脚本应该向我显示一个计数器,末尾有3
例如
这是可能的吗?我该怎么做?
我正在将Debian Linux与bash shell一起使用。以下代码将只查找section E 2 section count。尝试以下内容,并让我知道这是否有帮助
awk '/sectione 3/{a=""}/sectione 2/{a=1;next} !/#/ && a{count++} END{print count}' Input_file
EDIT1:现在也在同一个服务器上添加一个解决方案
awk '/sectione 2/,/sectione 3/{count=$0!~/#/?++count:count} END{print count}' Input_file
还有一种方法:
sed -n '/# sectione 2/,/# sectione 3/{/^#/d;//!p;}' file | wc -l
/section E 2/,/section E 3/选择section 2和section 3之间的线
/^/d删除以开头的行
//!!p打印其余的
sed -n '/sectione 2/,/sectione 3/{/^#/!p}' test.txt | wc -l
在行首处理Section E 2和Section E 3模式匹配之间的数据,然后打印任何不符合此模式的行!p、 您可以使用以下awk命令:
多行版本中的说明:
第2.awk节:
sed -n '/sectione 2/,/sectione 3/{/^#/!p}' test.txt | wc -l
awk '/sectione/{f=$3==2?1:0}f&&!/^#/{c++}END{print c}' file
/sectione/ {
# Set f(ound) to 1 (true) if the number between 'sectione' is a 2
# otherwise 0 (false)
f=$3==2?1:0
}
# If f(ound) is true (1) and the line starts not with a #
# count it
f&&!/^#/{
c++
}
# At the end of input print the c(ount)
END{
print c
}