此专题主要包含shell处理方面的技巧,如awk,sed等
1. 格式化文本
(1)有文本(file.txt),格式如下:
ad_click
1477569
1477569
ad_showtimes
1004446
1004446
ad
admin
35
35
想格式化为
ad_click 1477569 1477569
ad_showtimes 1004446 1004446
ad 0 0
admin 35 35
实现:
- awk -v RS= '{print $1,$2+0,$3+0}' file.txt
- or
- awk -v RS= '{print $1,$2?$2:0,$3?$3:0}' file.txt
(2). 四行合并一行
- # cat filename
- 111111111
- 222222222
- 333333333
- 444444444
- 555555555
- 666666666
- 777777777
- 888888888
- 999999999
- # awk '{if (NR%4==0){print $0} else {printf"%s ",$0}}' filename
- 111111111 222222222 333333333 444444444
- 555555555 666666666 777777777 888888888
- 999999999
- # awk '{if(NR%4!=0)ORS=" ";else ORS="\n";print}' filename
- or
- # awk '{if(NR%4==0)ORS="\n";else ORS=" ";print}' filename
(3)一个文本:
2006
中国
四川
042834 1 2 3
042835 4 5 6
042836 7 8 9
2007
中国
重庆
042837 1 2 3
042838 4 5 6
042839 7 8 9
......
......
要合并为
042834 1 2 3 2006 中国 四川
042835 4 5 6 2006 中国 四川
042836 7 8 9 2006 中国 四川
042837 1 2 3 2007 中国 重庆
042838 4 5 6 2007 中国 重庆
042839 7 8 9 2007 中国 重庆
脚本如下:
- #!/bin/bash
- #########################################################################
- # Author: pchuang@cn.ibm.com
- # Created Time: Sun 05 Apr 2009 10:04:51 AM CST
- # File Name: process.sh
- # Description: A script for the processing of the TEXT
- #########################################################################
- file=$1
- i=1
- lines[0]=""
- if [ ! -s $1 ] ; then
- echo "Please specify a valid text file"
- exit 1
- fi
- while read line
- do
- lines[$i]="$line"
- if [ $i -eq 6 ] ; then
- lines[$i]="$line"
- for j in $(seq 4 6); do
- echo ${lines[$j]} ${lines[1]} ${lines[2]} ${lines[3]} >> result.txt
- done
- let i=0
- fi
- let i=$i+1
- done < $1
执行:
$./process.sh text
$more result.txt
042834 1 2 3 2006 中国 四川
042835 4 5 6 2006 中国 四川
042836 7 8 9 2006 中国 四川
042837 1 2 3 2007 中国 重庆
042838 4 5 6 2007 中国 重庆
042839 7 8 9 2007 中国 重庆
后来某牛人用AWK也完成了:
- awk 'BEGIN{i=0}{if(NF!=4){h[i++]=$0;next}else{i=0;print $0" "h[0]" "h[1]" "h[2]}}' filename
- sed -n "/^[0-9]\+$/h;/^[^ 0-9]\+$/H;/ /{G;s/\n/ /g;p}" filename
(4) 两列合并成八列
111 111
222 222
333 333
444 444
555 555
666 666
777 777
888 888
999 999
101 101
实现:
- # xargs -n8 < filename
- 111 111 222 222 333 333 444 444
- 555 555 666 666 777 777 888 888
- 999 999 101 101
本日志由 flyinweb 于 2010-05-26 11:15:40 发表,目前已经被浏览 4200 次,评论 0 次;
引用通告:http://www.517sou.net/Article/450/Trackback.ashx
It is quite useful and interesting too.
VIRT 的上限是64G,也就是36位, cat /proc/cpuinfo的结果是:addre
昨天要准备用线程重写webbench,试验了下Fedora Linux 2.6.35.14
不明白您的具体的意思是什么?
已经发送到你QQ邮箱
http://www.2mysite.net/scriptencoder/screnc.asp 站长你好,看
你好,我发现一个问题,就是从mysqld2同步过来的数据,在mysqld1的
晕,我说是怎么回事情,原来我和你一样,忘记设置了活动分区