shell -2

sed 添加
sed ‘$r westos’ fstab 将 westos的内容加到 fstab的最后一行

[root@localhost mnt]# vim westos
123456789
      [root@localhost mnt]# sed '$r westos' fstab

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
123456789

sed ‘1r westos’ fstab 加到1行

[root@localhost mnt]# sed '1r westos' fstab

123456789
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

sed ‘r westos’ fstab 加到每行

[root@localhost mnt]#  sed 'r westos' fstab

123456789
#
123456789
# /etc/fstab
123456789
# Created by anaconda on Wed May  7 01:22:57 2014
123456789
#
123456789
# Accessible filesystems, by reference, are maintained under '/dev/disk'
123456789
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
123456789
#
123456789
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
123456789

sed -n -e ‘/^UUID/p’ fstab 显示 以UUID 开头的内容

[root@localhost mnt]# sed -n -e '/^UUID/p' fstab
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

sed -n -e ‘/^UUID/p’ fstab -e ‘/^UUID/=’ 显示内容 并加行号
-i 保存

[root@localhost mnt]#  sed -n -e '/^UUID/p' fstab -e '/^UUID/='
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
9

-f 和以上的一样

[root@localhost mnt]# vim file2
/^UUID/p
/^UUID/=
[root@localhost mnt]# sed -f file2 fstab 

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
9
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
[root@localhost mnt]# sed -n -f file2 fstab 
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
9

[root@localhost mnt]# sed 's/\#\ *//g' fstab


/etc/fstab
Created by anaconda on Wed May  7 01:22:57 2014

Accessible filesystems, by reference, are maintained under '/dev/disk'
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

sed ‘s/#\ *//g’ fstab s 替换 把#号和#号后空格 替换成空

[root@localhost mnt]# sed 's/\#\ *//g' fstab


/etc/fstab
Created by anaconda on Wed May  7 01:22:57 2014

Accessible filesystems, by reference, are maintained under '/dev/disk'
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

sed ‘/by/,/See/s/#\ *//g’ fstab 从by 到 See 替换

[root@localhost mnt]# sed '/by/,/See/s/\#\ *//g' fstab 

#
# /etc/fstab
Created by anaconda on Wed May  7 01:22:57 2014

Accessible filesystems, by reference, are maintained under '/dev/disk'
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
[root@localhost mnt]# vim westos
hello westos
hello westos
hello westos
hello westos
hello westos
hello westos
hello westos

添加空格

[root@localhost mnt]# sed 'G' westos
hello westos

hello westos

hello westos

hello westos

hello westos

hello westos

hello westos

最后一行没有空格

[root@localhost mnt]# sed '$!G' westos
hello westos

hello westos

hello westos

hello westos

hello westos

hello westos

hello westos

添加行号

[root@localhost mnt]# sed '=' westos
1
hello westos
2
hello westos
3
hello westos
4
hello westos
5
hello westos
6
hello westos
7
hello westos

替换空格

[root@localhost mnt]# sed '=' westos | sed 'N;s/\n//g'
1hello westos
2hello westos
3hello westos
4hello westos
5hello westos
6hello westos
7hello westos

查看最后一行

[root@localhost mnt]# sed '=' westos | sed 'N;s/\n//g' | sed -n '$p'
7hello westos

AWK 报告生成器

以 : 为分隔符 先打印NAME

[root@localhost mnt]# awk -F : 'BEGIN{print "NAME"}' /etc/passwd
NAME

然后打印第一个字符

[root@localhost mnt]# awk -F : 'BEGIN{print "NAME"}{ print $1 }' passwd
NAME
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp

最后打印 END

[root@localhost mnt]# awk -F : 'BEGIN{print "NAME"}{ print $1 }END{ print "END"}' passwd
NAME
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
END

以 /bash$ 结尾 打印第一个字符

[root@localhost mnt]# awk -F : '/bash$/{ print $1 }' passwd
root

以 root开头

[root@localhost mnt]# awk -F : '/^root/{ print $1 }' passwd
root

找到有用的 并统计有多少行

[root@localhost mnt]# awk -F : '/bash$/{ print $1 }END{print NR}' passwd
root
12

找到有用的 统计有多少列

[root@localhost mnt]# awk -F : '/bash$/{ print $1 }END{print NF}' passwd
root
7

查看第3行第一个字符

[root@localhost mnt]# awk -F : 'NR==3{ print $1 }' passwd
daemon

<=第三行的东西

[root@localhost mnt]# awk -F : 'NR<=3{ print $1 }' passwd
root
bin
daemon

= 3 且 <=5 行

[root@localhost mnt]# awk -F : 'NR>=3&&NR<=5{ print $1 }' passwd
daemon
adm
lp

统计可以登陆的用户

[root@localhost mnt]# awk 'BIGIN{N=0}/bash$/{N++}END{print N}' /etc/passwd
2

第 6 行 不在home的 并且以bash结尾的第一行字符

[root@localhost mnt]# awk -F : '$6!~/^\/home/&&/bash$/{ print $1}' /etc/passwd
root

统计个数

[root@localhost mnt]# awk -F : 'BEGIN{N=0}$6~/^\/home/&&/bash$/{N++}END{print N}' /etc/passwd
1

抓取 IP

[root@localhost mnt]# ifconfig eth0 | awk -F " " '/inet\>/{print $2}'
172.25.254.2

变量

输出用户

[root@localhost mnt]# echo $USER
root

输出 $1

[root@localhost mnt]# a=1
[root@localhost mnt]# echo $a
1

环境级别修改 export=1

[root@localhost mnt]# export a=1
[root@localhost mnt]# echo $a
1
[root@localhost mnt]# bash
[root@localhost mnt]# echo $a
1

vim .bash_profile 用户级别修改

[root@localhost mnt]# vim .bash_profile
export a=1
[root@localhost ~]# source .bash_profile 
[root@localhost ~]# echo $a
1

vim /etc/profile 系统级别

[root@localhost ~]# vim /etc/profile

export a=1
[root@localhost ~]# echo $a
1
[root@localhost ~]# su - student
[student@localhost ~]$ echo $a
1

env 查看变量

[root@localhost ~]# a=\$1
[root@localhost ~]# echo $a
$1
[root@localhost ~]# a='$1'
[root@localhost ~]# echo $a
$1

\ 转译单个字符
’ ’ 转译’ ’ 中的所有字符
” ” 弱引用,不能转译 \ $ !
$(date) 等同于
date`
1+2 的值

[root@localhost ~]# echo $[1+2]
3

${a}b ##区分显示{}内变量

[root@localhost ~]# a=1
[root@localhost ~]# echo ${a}b
1b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值