今天有学习了一些 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