LINUX文件操作的sed以及awk
一.Sed实验
原文件
1. sed -i 's/Jon/Jonathans/1' sed.txt
2.
3.sed -i 's/*Bath/Bath/g' sed.txt
sed -i 's/\/Milton/Milton/g' sed.txt
4.sed -i '/^$/d' sed.txt
5.sed -i '16,$d' sed.txt
6.
7.
二.awk实验1
对sed.txt操作
1.
2.
awk -F: 'BEGIN {print "Name Tel Salary"} $1 ~/teve Blenheim/ {print ($1,$2,$4)}' sed.txt
3.
awk -F: 'BEGIN {print "Name OfficeLocation"} $2 ~ /238/ {print ($1,$3)}' sed.txt
4.
awk -F: 'BEGIN {print "Salary"}{print ("$",$4)}' sed.txt
对awk的操作:
grade.awk:
BEGIN {
A = 0
B = 0
C = 0
D = 0
E = 0
F = 0
printf "Grade Name A B C D E F Average \n"
printf "-----------------------------------------------------\n"
}
function f(a){
if(a > 90)
printf "A"
else if(a > 80 && a < 90)
printf "B"
else if(a > 70 && a < 80)
printf "C"
else if(a > 60 && a < 70)
}
{
A+=$2
B+=$3
C+=$4
D+=$5
E+=$6
F+=$7
printf "%-8s %-8s %8d %8d %8d %8d %8d %8d %8d\n",f(($2+$3+$4+$5+$6+$7)/6),$1,$2,$3,$4,$5,$6,$7,($2+$3+$4+$5+$6+$7)/6
}
END{
printf "-----------------------------------------------------\n"
printf "Average:%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f\n", A/NR,B/NR,C/NR,D/NR,E/NR,F/NR
}