5月21号 晚上8点更新
employee文件中记录了工号和姓名
employee.txt:100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
bonus文件中记录工号和工资
bonus.txt:
100 $5,000
200 $500
300 $3,000
400 $1,250
要求把两个文件合并并输出如下
处理结果:
400 ashok sharma $1,250
100 jason smith $5,000
200 john doe $500
300 sanjay gupta $3,000
[root@fsailing1 shell]# vim employee.txt [root@fsailing1 shell]# vim bonus.txt [root@fsailing1 shell]# paste employee.txt bonus.txt 100 Jason Smith 100 $5,000 200 John Doe 200 $500 300 Sanjay Gupta 300 $3,000 400 Ashok Sharma 400 $1,250 [root@fsailing1 shell]# paste employee.txt bonus.txt |awk '{print $1 $2 $3 $ 5}' 100JasonSmith$5,000 200JohnDoe$500 300SanjayGupta$3,000 400AshokSharma$1,250[root@fsailing1 shell]# paste employee.txt bonus.txt |awk '{print $1,$2,$3,$5}' 100 Jason Smith $5,000 200 John Doe $500 300 Sanjay Gupta $3,000 400 Ashok Sharma $1,250[root@fsailing1 shell]# paste employee.txt bonus.txt |awk '{print $1 , $2 , $3 , $5}'|tr '[:lower:]' '[:upper:]' 100 JASON SMITH $5,000 200 JOHN DOE $500 300 SANJAY GUPTA $3,000 400 ASHOK SHARMA $1,250 [root@fsailing1 shell]# paste employee.txt bonus.txt |awk '{print $1 , $2 , $3 , $5}'|tr '[:upper:]' '[:lower:]'|sort -k 2 400 ashok sharma $1,250 100 jason smith $5,000 200 john doe $500 300 sanjay gupta $3,0005月22号 下午2点
文件格式如下:
100
a 100
b -50
c -20
d -30
要求输出结果为:
100
a 100
200
b -50
150
c -20
130
d -30
100
[root@fsailing1 shell]# awk '{print $0;print $1}' test.txt 100 100 a 100 a b -50 b c -20 c d -30 d [root@fsailing1 shell]# awk 'NR==1{sum=$1;print $0}' test.txt NR表示awk读到的文件行数。 100 [root@fsailing1 shell]# awk 'NR==1{sum=$1;print $0} NR!=1{print $0;print sum+=$2}' test.txt 100 a 100 200 b -50 150 c -20 130 d -30 100