sed和awk常用用法(学习2)
2011-12-16 13:43
471 查看
今天有学习了一些 http://bbs.linuxtone.org/thread-1242-1-2.html 记录一下了
1.awk给输出的字段加单引号。
[root@localhost test]# awk -v aa="'" '{print $1,aa $2 aa}' t1
a '1'
b '45'
2 删除两列之间多个不等空格使成为两列中间只有一个空格的规范格式
[root@localhost test]# cat t1
a 1
b 45
c 90089809
d dsd
[root@localhost test]# cat t1 | tr -s ' '
a 1
b 45
c 90089809
d dsd
3.将一列的转换成一行(本例是指第一列,也同理换成其他列)
[root@localhost test]# cat t1
a 1
b 45
c 90089809
d dsd
[root@localhost test]# awk '{printf "%s",$1}' t1
abcd
4.用awk给定的分隔符格式化输出
[root@localhost test]# echo a b c|awk '{print $1":"$2":"$3}'
a:b:c
[root@localhost test]# echo a b c|awk '{OFS=":";print $1,$2,$3}'
a:b:c
5.从右取字符串中的字符操作
[root@localhost test]# a="abcdefg"
[root@localhost test]# echo ${a:(-3)}
efg
[root@localhost test]# echo ${a:(-1)}
g
[root@localhost test]# echo ${a:(2)}
cdefg
6tr命令:
tr "[a-z]" "[A-Z]" 转换大小写
tr "@" "\n" 替换
tr -d "a" 删除
tr -s "a" 相邻重复的a 只保留一个
[root@localhost test]# echo abcde |tr -d "a"
bcde
[root@localhost test]# echo abcde |tr "a" "#"
#bcde
[root@localhost test]# echo aaabcde |tr -s "a"
abcde
7指定每一行的第几次出现进行替换
[root@localhost test]# echo abcdefabc | sed 's/abc/ABC/1'
ABCdefabc
[root@localhost test]# echo abcdefabc | sed 's/abc/ABC/2'
abcdefgABC111
8 打印文件的奇数行
[root@localhost test]# sed -n 'p;n' t1
1 a 1
3 c 90089809
[root@localhost test]# sed 'n;d' t1
1 a 1
3 c 90089809
9 打印文件的偶数行
[root@localhost test]# sed -n 'n;p' t1
2 b 45
4 d dsd
1.awk给输出的字段加单引号。
[root@localhost test]# awk -v aa="'" '{print $1,aa $2 aa}' t1
a '1'
b '45'
2 删除两列之间多个不等空格使成为两列中间只有一个空格的规范格式
[root@localhost test]# cat t1
a 1
b 45
c 90089809
d dsd
[root@localhost test]# cat t1 | tr -s ' '
a 1
b 45
c 90089809
d dsd
3.将一列的转换成一行(本例是指第一列,也同理换成其他列)
[root@localhost test]# cat t1
a 1
b 45
c 90089809
d dsd
[root@localhost test]# awk '{printf "%s",$1}' t1
abcd
4.用awk给定的分隔符格式化输出
[root@localhost test]# echo a b c|awk '{print $1":"$2":"$3}'
a:b:c
[root@localhost test]# echo a b c|awk '{OFS=":";print $1,$2,$3}'
a:b:c
5.从右取字符串中的字符操作
[root@localhost test]# a="abcdefg"
[root@localhost test]# echo ${a:(-3)}
efg
[root@localhost test]# echo ${a:(-1)}
g
[root@localhost test]# echo ${a:(2)}
cdefg
6tr命令:
tr "[a-z]" "[A-Z]" 转换大小写
tr "@" "\n" 替换
tr -d "a" 删除
tr -s "a" 相邻重复的a 只保留一个
[root@localhost test]# echo abcde |tr -d "a"
bcde
[root@localhost test]# echo abcde |tr "a" "#"
#bcde
[root@localhost test]# echo aaabcde |tr -s "a"
abcde
7指定每一行的第几次出现进行替换
[root@localhost test]# echo abcdefabc | sed 's/abc/ABC/1'
ABCdefabc
[root@localhost test]# echo abcdefabc | sed 's/abc/ABC/2'
abcdefgABC111
8 打印文件的奇数行
[root@localhost test]# sed -n 'p;n' t1
1 a 1
3 c 90089809
[root@localhost test]# sed 'n;d' t1
1 a 1
3 c 90089809
9 打印文件的偶数行
[root@localhost test]# sed -n 'n;p' t1
2 b 45
4 d dsd
相关文章推荐
- LINUX的awk和sed的常用用法
- sed常用用法(学习)
- 转 -- 【Shell】【经验】awk sed grep find sort常用配搭用法
- 【shell脚本】常用工具的学习grep、sed、awk、cut
- 转载自马哥视频学习笔记---Linux Web服务器网站故障分析常用的命令之awk用法的简单示例
- LINUX的awk和sed的常用用法
- shell学习第三天——grep、sed、awk
- sed && awk学习总结
- shell学习之-sed用法解析
- awk常用函数用法
- Shell中的grep、awk和sed的常用命令和语法
- LVS的持久连接和awk/sed/grep的学习笔记
- linux下grep、awk和sed工具用法概览
- GREP、SED、AWK、TR、SORT、UNIQ的一些用法
- sed|grep|awk常用过滤log
- sed和awk的常用实例
- sed和awk的常用实例
- rpm、yum、字符串学习记录常用用法
- awk的几个常用高级用法
- sed和awk入门以及vim常用操作