牛客网在线编程SHELL篇

本文详细介绍了多种Shell脚本的使用方法,包括统计文件行数、打印文件内容、查找特定模式、处理数字和字符串、分析日志文件等。通过这些实例,读者可以深入理解并掌握Shell脚本在实际问题解决中的应用。

SHELL1 统计文件的行数

cat nowcoder.txt | wc -l

SHELL2 打印文件的最后5行

tail -n 5 nowcoder.txt

SHELL3 输出7的倍数

seq 0 7 500

SHELL4 输出第5行的内容

sed -n '5p' nowcoder.txt

SHELL5 打印空行的行号

grep -nv '.' nowcoder.txt  | cut -d ':' -f 1

SHELL6 去掉空行

grep '.' nowcoder.txt

SHELL7 打印字母数小于8的单词

for word in `cat nowcoder.txt`
do
    if [ ${#word} -lt 8 ]; then
        echo $word
    fi
done

SHELL8 统计所有进程占用内存大小的和

total_mem=0
for mem in `awk '{print $6;}' nowcoder.txt`
do
	total_mem=$[$total_mem + $mem]
done
echo $total_mem

SHELL9 统计每个单词出现的个数

awk '{ for (i = 1; i <= NF; i++) { print $i } }' nowcoder.txt | sort | uniq -c | sort -n | awk '{ print $2, $1 }'

SHELL10 第二列是否有重复

awk '{print $2}' nowcoder.txt | sort | uniq -cd | sort -n

SHELL11 转置文件的内容

num_columns=`head -n 1 nowcode
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值