情况如下:有多个 *.txt 文件,每个文件都包含以下具有不同值var
或 的行lab
。
例如:
abc.txt
:
var^ABCDEFG
lab^ABCDEFGH
def.txt
:
var^ABCDEFGHI
lab^ABCDEFGHIJ
我需要一个命令或脚本来打印每个 .txt 文件的(var^
或)中的字符数总和。lab^
示例输出:
abc.txt: Total Characters in (Var and Lab) are 15. (counting character after the caret ^ sign)
def.txt: Total Characters in (Var and Lab) are 19.
答案1
将@steeldriver的答案扩展到其他awk
没有的实现,BEGINFILE
并ENDFILE
使其变得有点混乱,但可移植:
awk -F'^' 'FNR==1{if (NR>FNR) printf("%s : Total characters in (Var) and (Lab) are %d\n",lastfile,sum); sum=0; lastfile=FILENAME} \
NF==2 && ($1=="var" || $1=="lab") {sum+=length($2)} \
END{printf "%s: Total characters in (Var) and (Lab) are %d\n", FILENAME, sum}' abc.txt def.txt
解释:
在文件开头(即
FNR
每文件行计数器为 1),我们将文件名存储在临时变量中lastfile
,并将计数器变量设置为 0。如果这不是第一个文件(即
NR
全局行计数器大于FNR
),我们输出前一个文件的统计信息。由于此规则不会捕获
awk
参数列表中的最后一个文件,因此我们还必须在全局块中复制逻辑END
。
答案2
像这样的东西应该有效:
for file in *.txt; do \
echo -n "$file: Total Characters in (Var and Lab) are ";
cut -d "^" -f 2 < $file | tr -d '\n' | wc -c;
done
输出:
abc.txt: Total Characters in (Var and Lab) are 15
def.txt: Total Characters in (Var and Lab) are 15
答案3
你可以使用 GNU awk 这样做:
gawk -F^ '
BEGINFILE{count=0}
$1 == "var" || $1 == "lab" {count += length($2)}
ENDFILE{printf("%s: Total Characters in (Var and Lab) are %d\n", FILENAME, count)}
' ???.txt
abc.txt: Total Characters in (Var and Lab) are 15
def.txt: Total Characters in (Var and Lab) are 19
答案4
使用grep
和wc
:
for i in *.txt; do
printf '%s: Total Characters in (Var and Lab) are %d.\n'\
"$i"\
"$(( $(grep -E '^(var|lab)' "$i" | wc -c) - 10 ))"
done