shell文本文件的处理(2)
时间:2007-05-17 来源:zhouliming9
1、在文本每行前增加一行
文本a.txt
line1 table1
line2 table2 line1 table3
line2 table4
要求每一行修改成
print table1
line1 table1
print table2
line2 table2
print table3
line3 table3
print table4
line4 table4 方法: cat a.txt |awk 'BEGIN{FS=""}{for(i=1;i<=NF;i++)print $(i)}' | awk '{print "print " $2 "\n" $1 " "$2}' 或者: cat a.txt |awk '{print "print "$2"\n" $0}'
sed 'h;s/[^ ]*/print/;G'
sed 's/\([^ ]*\) .*$/print \1\n&/' 2、从文本tt.txt 2007-04-29 15:41:54 [Debug][NULL] Param URL:http://www.chinaunix.com
2007-04-29 15:41:54 [Debug][NULL] 02. range:bytes=45-491
2007-04-29 15:41:54 [Debug][NULL] 09. dtuserid:ddddddddddddddddddddddd
2007-04-29 15:41:54 [Debug][NULL] Param URL:http://bbs.chinaunix.com
2007-04-29 15:41:54 [Debug][NULL] 04. range:bytes=0-321
2007-04-29 15:41:54 [Debug][NULL] 07. dtuserid:aaaaaaaaaaaaaaaaaaaaaaa 上面的日志3行为一组,当range:bytes=0-*时,取Param URL的值和dtuserid的值
上面的日志,我最终想要的结果是 2007-04-29 15:41:54 http://bbs.chinaunix.com aaaaaaaaaaaaaaaaaaaaaaa 方法: tt=xx.txt
a=`awk '/dtuserid:aaaaaaaaaaaaaaaa/ {print NR}' $tt`
b=`awk '/dtuserid:aaaaaaaaaaaaaaaa/ {print NR-2}' $tt`
sed -n ${b},${a}p $tt >abc.txt
c=`cat abc.txt |awk '{print $1" "$2}'|uniq`
d=`grep "Param URL" abc.txt |awk -F ':' '{print $4":"$5}'`
e=`grep dtuserid abc.txt |awk -F ':' '{print $4}'`
echo $c $d $e
rm abc.txt
line1 table1
line2 table2 line1 table3
line2 table4
要求每一行修改成
print table1
line1 table1
print table2
line2 table2
print table3
line3 table3
print table4
line4 table4 方法: cat a.txt |awk 'BEGIN{FS=""}{for(i=1;i<=NF;i++)print $(i)}' | awk '{print "print " $2 "\n" $1 " "$2}' 或者: cat a.txt |awk '{print "print "$2"\n" $0}'
sed 'h;s/[^ ]*/print/;G'
sed 's/\([^ ]*\) .*$/print \1\n&/' 2、从文本tt.txt 2007-04-29 15:41:54 [Debug][NULL] Param URL:http://www.chinaunix.com
2007-04-29 15:41:54 [Debug][NULL] 02. range:bytes=45-491
2007-04-29 15:41:54 [Debug][NULL] 09. dtuserid:ddddddddddddddddddddddd
2007-04-29 15:41:54 [Debug][NULL] Param URL:http://bbs.chinaunix.com
2007-04-29 15:41:54 [Debug][NULL] 04. range:bytes=0-321
2007-04-29 15:41:54 [Debug][NULL] 07. dtuserid:aaaaaaaaaaaaaaaaaaaaaaa 上面的日志3行为一组,当range:bytes=0-*时,取Param URL的值和dtuserid的值
上面的日志,我最终想要的结果是 2007-04-29 15:41:54 http://bbs.chinaunix.com aaaaaaaaaaaaaaaaaaaaaaa 方法: tt=xx.txt
a=`awk '/dtuserid:aaaaaaaaaaaaaaaa/ {print NR}' $tt`
b=`awk '/dtuserid:aaaaaaaaaaaaaaaa/ {print NR-2}' $tt`
sed -n ${b},${a}p $tt >abc.txt
c=`cat abc.txt |awk '{print $1" "$2}'|uniq`
d=`grep "Param URL" abc.txt |awk -F ':' '{print $4":"$5}'`
e=`grep dtuserid abc.txt |awk -F ':' '{print $4}'`
echo $c $d $e
rm abc.txt
相关阅读 更多 +