How to use a Shell script to count the number of lines in a file?

You can use the following command to count the number of lines in a file:

#!/bin/bash

file="example.txt"  # 文件名

# 使用wc命令统计文件的行数并保存到变量中
line_count=$(wc -l < "$file")

echo "文件 $file 中的行数为: $line_count"

Save the above code as a file called count_lines.sh and give it execute permissions. Then run the script in the terminal.

chmod +x count_lines.sh
./count_lines.sh

The above command will display the number of lines in the file example.txt. You can replace example.txt with the name of the file you want to count the lines of.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds