自己写的小东西,也是课后实验。
直接拷贝可能无法运行,会出现未知字符错误,这个小错误浪费了我好多时间。
#! /bin/sh
n=1
function insert(){
read str
sed -i '1i\'"$str" score.txt //插入输入变量str到文件的第一行(文件无内容可能要自己在文件添加记录,否则这条命令无效)
}
function modify(){
read str
read str1
read str2
sed -i "/$str/ s/$str1/$str2/" score.txt //第一个参数是包含str的行,用str2替换str1
}
function display(){
sed -n '1,$p' score.txt //显示所有行
}
function statics(){
awk '{print $0,$1,$3 | "sort -r -k 3"}' score.txt |head -10 //对第三个字段排序,取前10
}
function search(){
read str
awk "/$str/" score.txt //查找包含str的行
}
function delete(){
read str
sed -i "/^$str/d" score.txt //删除包含str参数的行
}
function total(){
awk '{ total=$3+$4 | print $0 total | head -20 }' score.txt
}
while [ "$n" > 0 ];
do
echo "=================