Linux Commands: sed and awk Commands

Delete the first 6 characters of each line in the file:

$sed -i.bak 's/^.\{6\}//g' urfile

Remove spaces at the beginning of each line of the file:

$awk '{sub(/^[ \t]+/,"");print $0}' filename #方法1
$sed -i 's/^[ ]*//g' filename #Method 2

View the last 5 logged in users and ip addresses:

$ last -n 5|awk '{print $1"\t"$3}'
Toa	:0
Toa	:0
reboot	boot
Toa	:0
Toa	:0
wtmp	Wed

Look up the data below 10 in the third column:

$ cat /etc/passwd|awk '{FS=":"} $3<10 {print $0}'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

Statistical data:

$ cat score.txt
Name    math    english   gym
Rong 98 100 99
Tao 99 100 99
RT      97      100       99
$ cat score.txt  |awk 'NR==1{printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,"Total"} \
NR>=2{total=$2+$3+$4
printf "%10s %10d %10d %10d %10.2f\n",$1,$2,$3,$4,total score}'
      Name       math    english        gym      Total
      Rong 98 100 99 297.00
       People 99 100 99 298.00
        RT         97        100         99     296.00
More instructions: http://man.linuxde.net/sed

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325853550&siteId=291194637