Awk 打印一个文件的行数和另一个文件的行总数

Awk 打印一个文件的行数和另一个文件的行总数,awk,Awk,我有一个名为Stem的目录,在该Stem目录中,我有两个名为result.txt和title.txt的文件,如下所示: result.txt: Column1 Column2 Column3 Column4 ---------------------------------------------------------- Setup First Second Third Setdown Fifth Sixth

我有一个名为Stem的目录,在该Stem目录中,我有两个名为result.txt和title.txt的文件,如下所示:

result.txt:

Column1     Column2     Column3     Column4
----------------------------------------------------------
Setup       First       Second      Third
Setdown     Fifth       Sixth       Seven   
setover     Eight       Nine        Ten
Setxover    Eleven      Twelve      Thirteen
Setdrop     Fourteen    Fifteen     sixteen
title.txt:

Column1     Column2     Column3     Column4
----------------------------------------------------------
result        20           40         60
result1       40           80         120
Total:        60           120        180
我需要计算第一个fileresult.txt中除前两行之外的行数,从第二个filetitle.txt中我需要行总数列3中的数据,我需要得到如下输出:

Stem : 5    120
我使用了这个脚本,但没有得到确切的输出

#!/bin/bash
for d in stem;
do
echo "$d"
File="result.txt"
File1="title.txt"
awk 'END{print NR - 2}' "$d"/"$File" 
awk '/Total/{print $(NF-1);exit}' "$d"/"$File1"
done
编辑:由于OP的问题不清楚到底需要哪个值,上一个答案提供了第二列的总和,如果OP需要得到包含Total:关键字的行的最后第二个字段值,请尝试以下操作:

awk '
FNR==NR{
  tot=FNR
  next
}
/Total:/{
  sum=$(NF-1)
}
END{
  print "Stem : ",tot-2,sum+0
}
' result.txt title.txt
说明:增加对以上内容的详细说明

awk '                              ##Starting awk program from here.
FNR==NR{                           ##Checking condition FNR==NR which will be TRUE when result.txt is being read.
  tot=FNR                          ##Creating tot which has value of FNR in it.
  next                             ##next will skip all further statements from here.
}
/Total:/{                          ##Checking condition if line contains Total: then do following.
  sum=$(NF-1)                      ##Creating sum which has 2nd last field of current line.
}
END{                               ##Starting END block of this program from here.
  print "Stem : ",tot-2,sum+0      ##Printing Stem string tot-2 and sum value here.
}
' result.txt title.txt            ##Mentioning Input_file names here.
编辑:由于OP的问题不清楚到底需要哪个值,上一个答案提供了第二列的总和,如果OP需要得到包含Total:关键字的行的最后第二个字段值,请尝试以下操作:

awk '
FNR==NR{
  tot=FNR
  next
}
/Total:/{
  sum=$(NF-1)
}
END{
  print "Stem : ",tot-2,sum+0
}
' result.txt title.txt
说明:增加对以上内容的详细说明

awk '                              ##Starting awk program from here.
FNR==NR{                           ##Checking condition FNR==NR which will be TRUE when result.txt is being read.
  tot=FNR                          ##Creating tot which has value of FNR in it.
  next                             ##next will skip all further statements from here.
}
/Total:/{                          ##Checking condition if line contains Total: then do following.
  sum=$(NF-1)                      ##Creating sum which has 2nd last field of current line.
}
END{                               ##Starting END block of this program from here.
  print "Stem : ",tot-2,sum+0      ##Printing Stem string tot-2 and sum value here.
}
' result.txt title.txt            ##Mentioning Input_file names here.

目录名是Stem还是Stem?在类Unix系统上,大写字母很重要。您得到的输出是什么?目录名为Stem还是Stem?在类Unix系统上,大写字母很重要。你得到的输出是什么?。你的脚本中有一个疑问…这个…FNR==NR{tot=FNR next….是第一个文件,而/Total:/{sum=$NF-1是第二个文件…我说的对吗?@Rama,是的,你得到了:没错,FNR==NR只对第一个输入文件是正确的,干杯……你的脚本中有一个疑问…这个…FNR==NR{tot=FNR next…..是第一个文件,/Total:/{sum=$NF-1是第二个文件…我说的对吗?@Rama,是的,你明白了:没错,FNR==NR只对第一个输入文件是正确的,干杯。