- 打印nowcoder.txt文件的最后5行
tail -5 1.txt
- 输出7的倍数
for i in {0..500..7}
do
echo $i
done
- 统计nowcoder.txt文件的行数
cnt=0;
while read val
do
cnt=$((cnt+1))
done < nowcoder.txt
echo $cnt
- 输出nowcoder.txt第5行的内容
line=0
while read var;
do
line=$((line+1))
if [ $line -eq 5 ];
then
echo $var
fi
done < nowcoder.txt
- 打印空行的行号
line=1;
while read val;
do
if [ -z $val ];
then
echo $line;
fi
line=$((line+1));
done < nowcoder.txt
- 去掉空行
while read line
do
if [ ! -z $line ];
then
echo $line;
fi
done < nowcoder.txt
- 打印字母数小于8的单词
#!/bin/sh
read line < nowcoder.txt
for i in $line
do
if [ ${#i} -lt 8 ]
then
echo $i
fi
done
- 统计所有进程占用内存和的大小
ps aux > nowcoder.txt
awk '{sum=sum+$6} END{print sum}' nowcoder.txt
- 求平均值
test.txt
4
1
2
9
8
awk '{
if(NR==1) total=$1;
else sum+=$1
}
END{
printf("%.3f",sum/total);
}' $1 test.txt
- 查看内存
ps -ef
ps aux
top