1.awk给输出的字段加单引号。(下面输出的第三个字段都加上了引号)awk -v aa="'" '{print $1,$2, aa $3 aa}' filename例子:[root@WANGJIAN ~]# awk -v aa="'" '{print $1,aa $2 aa}' filea '13'b '1'2.删除两列之间多个不等空格使成为两列中间只有一个空格的规范格式方法1:cat filename |tr -s ' ' 方法2: cat filename |sed 's/ \{2,\}/ /g'例子:[root@WANGJIAN ~]# cat aa 13b 1c 23d 234[root@WANGJIAN ~]# cat a | tr -s ' ' #注意引号里有空格a 13b 1c 23d 234[root@WANGJIAN ~]# cat a | sed 's/ \{2,\}/ /g' #注意空格a 13b 1c 23d 2343.将一列的转换成一行(本例是指第一列,也同理换成其他列)awk '{printf "%s",$1}' filename例子:[root@WANGJIAN ~]# cat filea 13b 1c 23d 234[root@WANGJIAN ~]# awk '{printf "%s", $1}' fileabcd补充:合并上下行并以空格相分隔sed '$!N;s/\n/ /' #合并上下两行cat file12345678900987654321执行命令后1234567890 09876543214.用awk给定的分隔符格式化输出[root@WANGJIAN ~]# echo a b c |awk '{print $1":"$2":"$3}'a:b:c[root@WANGJIAN ~]# echo a b c |awk '{OFS=":";print $1,$2,$3}'a:b:c5.用某一文件的一个域替换另一个文件中的的特定域(条件是两个文件第一列相同)该例是用linux shadow文件中的密文部分替换passwd中的"x",生一个新passwd文件,如下所示文件passwd: s2002408030068:x:527:527::/home/dz02/s2002408030068:/bin/pw 文件shadow: s2002408030068:$1$d8NwFclG$v4ZTacfR2nsbC8BnVd3dn1:12676:0:99999:7::: awk 'BEGIN{OFS=FS=":"} NR==FNR{a[$1]=$2}NR>FNR{$2=a[$1];print}' shadow passwdNR==FNR,第一个文件shadow,以$1为下标,将$2的值赋给数组aNR>FNR,第二个文件passwd,将文件shadow$2的值赋值给文件passwd6.编写一个sed命令,给一个文件的行间距变为两倍行距sed '$!G;$!G' filename7.常见sed等价命令basename sed 's/\(.*\)\/\([^/]*\)/\2/' or sed 's,.*/,,'cat sed '' or sed -n '1,$p' or sed '1,$!d'cat -s sed '/./,/^$/!d'cat -n sed '=' | sed 'N;s/\n/\t/;s/^/ &/' or sed '=' | sed '$!N;s/\n/ /'cat -E sed 's/$/\$/'cat -t sed 's/\t/^I/g'cut -c n sed 's/\(.\)\{n\}.*/\1/' or sed 's/^.\{(n-1)\}//g;s/\(.\)\(.*\)/\1/g'cut -c x-y sed 's/\(^.\{y\}\)\(.*\)/\1/g;s/^.\{(x-1)\}//'cut -d| -f6 sed 's/\(\([^|]*\)\|\)\{6\}.*/\2/'cp file1 file2 sed 'w file2' file1expand -t 1 sed 's/\t/ /g'dirname sed 's/\(.*\)\/\([^/]*\)/\1/' or sed 's,[^/]*$,,'grep patten sed -n '/patten/p' or sed '/patten/!d'grep -v patten sed -n '/patten/!p' or sed '/pateen/d'grep -n patten sed -n '/patten/{=;p}'| sed 'N;s/\n/:/'head sed -n '1,10p'head -1 sed -n '1p' or sed 'q'head -Number sed '1,Number!d' or sed 'Numberq'paste -s file1 file2 sed ':a;N;s/\n/\t/;ba;' file1 file2 | sed 's/\t\t/\n/'paste -sdstr sed ':a;N;s/\n/str/;ba'rev sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'tac sed -n '1! G;$p;h' or sed -n 'G;$p;h'tail -1 sed -n '$p' or sed '$!d'tail -Number sed ':t;$q;N;(Number+1),$D;bt'tail -f sed -u '/./!d'tr "\n" " " sed ':a;N;s/\n/ /;ba'tr "A-Z" "a-z" sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'tr "a-z" "A-Z" sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'8.将字符串进行割接显示如将 hellohellohello 显示为hellohellohello方法:echo "hellohellohello" |fold -w5echo hellohellohello|sed 's/\(.....\)/\1\n/g'echo hellohellohello|sed 's/\(.....\)/\1#/g'|tr '#' '\n'9.从右取字符串中的字符操作[root@localhost ~]# string="abcdefg"[root@localhost ~]# echo ${string:(-3)} #取efgefg[root@localhost ~]# echo ${string:(-1)}g[root@localhost ~]# echo ${string:(2)} #跳过前2个字符cdefg10.汉字按拼音排序[root@localhost ~]# cat str 的见上我人欧怕我下风那把长下在懊上的风个还见看 #未排序前[root@localhost ~]# cat str |sed 's/./&\n/g'|LC_ALL=C sort|tr '\n' ' '|tr -d ' ';echo#懊把长的的风风个还见见看那欧怕排前人上上未我我下下序在11.tr命令小结tr命令:tr "[a-z]" "[A-Z]" 转换大小写tr "@" "\n" 替换tr -d "a" 删除tr -s "a" 相邻重复的a 只保留一个[root@localhost ~]# echo abcde |tr -d "a"bcde[root@localhost ~]# echo abcde |tr "a" "#"#bcde[root@localhost ~]# echo aabcded |tr -s "a"abcded12.指定每一行的第几次出现进行替换sed 's/old/new/n' filename[root@localhost ~]# cat shabcdddsadsfsfdiodsabcdljlsabc[root@localhost ~]# cat sh|sed 's/abc/ABC/1'ABCdddsadsfsfdiodsABCdljlsabc[root@localhost ~]# cat sh|sed 's/abc/ABC/2'abcdddsadsfsfdiodsabcdljlsABC[root@localhost ~]# 13. 为文件加行号输出sed = filename|sed 'N;s/\n/:/'cat -n filenamenl filename[root@localhost ~]# sed = /etc/passwd|sed 'N;s/\n/:/' #可以指定分割符1:root:x:0:0:root:/root:/bin/bash2:bin:x:1:1:bin:/bin:/sbin/nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin[root@localhost ~]# sed = /etc/passwd|sed 'N;s/\n/ /'1 root:x:0:0:root:/root:/bin/bash2 bin:x:1:1:bin:/bin:/sbin/nologin14.打印文档奇数行(隔行输出)sed 'n;d'sed 'x;$!N;x'sed -n 'p;n'135715.打印偶数行(隔行输出)sed -n 'n;p'sed '1d;n;d;'246816.用于计算的两个命令 bc exprexpr 2 + 3 两个数和运算符之间要有空格[root@localhost ~]# date +%d-2|bc 前两天的日18计算的时候要有小数部分:echo "2/8" |bc -lecho $a*$b|bcecho "scale=2; 2/8" | bc -lawk 'BEGIN{printf "%.2f",2/8}' 可以指定保留小数点几位
posted on 2012-04-24 23:26
kxbin 阅读(591)
评论(0) 编辑 收藏 所属分类:
转发 、
Linux