首页 > 试题广场 >

统计文件的行数

[编程题]统计文件的行数
  • 热度指数:118640 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
编写一个shell脚本以输出一个文本文件nowcoder.txt中的行数
示例:
假设 nowcoder.txt 内容如下:
#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    int b = 100;
    cout << "a + b:" << a + b << endl;
    return 0;
}
你的脚本应当输出:
9
示例1

输入

#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    int b = 100;
    cout << "a + b:" << a + b << endl;
    return 0;
}

输出

9
#!/bin/bash

wc -l < nowcoder.txt

发表于 2025-06-06 10:55:42 回复(0)
sed -n p nowcoder.txt |wc -l
发表于 2025-05-23 14:28:46 回复(0)
cat nowcoder.txt |grep -c [^\s]
发表于 2025-04-16 11:37:29 回复(0)
wc -l < nowcoder.txt
发表于 2024-07-02 11:55:14 回复(0)
cat nowcoder.txt | wc -l
发表于 2023-06-02 16:51:24 回复(0)
#!/bin/bash
awk '{print NR}' nowcoder.txt | tail -1
发表于 2023-02-07 12:05:02 回复(0)
count=0
while read line
do
    count=$[ $count + 1 ]
done
echo $count
发表于 2022-09-02 09:44:26 回复(0)
cat nowcoder.txt |wc -l
awk 'END{print FNR}' nowcoder.txt 
wc -l nowcoder.txt | awk '{print $1}' 
wc -l < nowcoder.txt
发表于 2022-08-27 17:03:41 回复(0)
wc -l < nowcoder.txt

发表于 2022-08-16 18:45:48 回复(0)
while read line; do
     ((item++))
done  < nowcoder.txt
echo $item
发表于 2022-08-11 12:07:49 回复(0)
echo `wc -l nowcoder.txt` | awk '{print $1}'
发表于 2022-08-08 17:36:34 回复(0)
grep . -c nowcoder.txt
发表于 2022-07-26 20:01:45 回复(0)
wc -l < nowcoder.txt
发表于 2022-07-14 20:25:26 回复(0)
先用 cat查看文件内容 将结果传给 wc -l 计算行数
cat nowcoder.txt | wc -l
发表于 2022-07-13 09:23:58 回复(0)
i=0
while read line
do
    let i++
done < nowcoder.txt
echo "$i"
发表于 2022-07-04 23:12:57 回复(0)
#!/bin/bash
int=0
while read line
do
    let "int++"
done < nowcoder.txt
echo $int
发表于 2022-07-04 13:24:27 回复(0)
#/bin/bash
a=0
while read line;do
   if [[ -n $line ]];then
       let a+=1
    fi
done < nowcoder.txt
echo $a
发表于 2022-06-13 21:20:23 回复(1)