嵌入式 文件中获取想要字符串初试牛刀sed与awk结合

awk打印最后一个(每行)

ls -lsh | awk '$NF {print $NF}'

load.log文件内容,我需要的内容是获取下载进度 :

--2013-12-11 15:46:47--  http://www.sina.com/
Resolving www.sina.com (www.sina.com)... 202.108.33.60
Connecting to
www.sina.com (www.sina.com)|202.108.33.60|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location:
http://www.sina.com.cn/ [following]
--2013-12-11 15:46:48-- 
http://www.sina.com.cn/
Resolving www.sina.com.cn (www.sina.com.cn)... 202.108.33.60
Reusing existing connection to
www.sina.com:80.
HTTP request sent, awaiting response... 200 OK
Length: 634880 (620K) [text/html]
Saving to: `index.html'
     0K .......... .......... .......... .......... ..........  8%  370K 2s
    50K .......... .......... .......... .......... .......... 16%  984K 1s
   100K .......... .......... .......... .......... .......... 24% 1019K 1s
   150K .......... .......... .......... .......... .......... 32% 1.00M 1s
   200K .......... .......... .......... .......... .......... 40% 1000K 0s
   250K .......... .......... .......... .......... .......... 48%  975K 0s
   300K .......... .......... .......... .......... .......... 56% 1010K 0s
   350K .......... .......... .......... .......... .......... 64% 1.02M 0s
   400K .......... .......... .......... .......... .......... 72% 1023K 0s
   450K .......... .......... .......... .......... .......... 80% 1.01M 0s
   500K .......... .......... .......... .......... .......... 88% 1.02M 0s
   550K .......... .......... .......... .......... .......... 96%  940K 0s
   600K .......... ..........                                 100% 1.15M=0.7s
2013-12-11 15:46:49 (889 KB/s) - `index.html' saved [634880/634880]

 

有时候可能下载过程中没有下载完成,即:

1、
   600K .......... ..........                                100% 1.15M=0.7s
2013-12-11 15:46:49 (889 KB/s) - `index.html' saved [634880/634880]

2、


   600K .......... ..........  ................................................                               100% 1.15M=0.7s
2013-12-11 15:46:49 (889 KB/s) - `index.html' saved [634880/634880]

 

3、

0s
   500K .......... .......... .......... .......... .......... 88% 1.02M 0s
   550K .......... .......... ........

这三种可能都会出现,那怎么兼容就是问题所在了:

初步测试我脚本可以完成兼容:

kj.sh:

#!/usr/local/env sh
#wget -c
ftp://ftp.kernel.org/pub/linux/kernel/v3.x/patch-3.10.gz -o load.log &
while true
do
#sed -n '/K\>/p' load.log | sed '$d' | sed -n '$p' | awk '{print$7}'
#sed -n '/s$/p' load.log | sed '$d' | sed -n '$p' | awk '{print$7}'
#sed -n '/K\>/p' load.log
#sed -n '/..........\>/p' load.log
#sed -n '/..........\>/p' load.log | sed '$d' | sed -n '$p' | sed 's/.* .*K.*\. //' | awk '{print$1}'
#sed -n '/..........\>/p' load.log | sed '$d' | sed -n '$p' | sed -n "s/.*K.* *\.[ \t]\(.*\)$/\1/p" log | awk '{print$1}'
sed -n '/..........\>/p' load.log | sed '$d' | sed -n '$p' | sed -n "s/.*K.* *\.[ \t]\(.*\)$/\1/p" | awk '{print$1}'
sleep 1s
done

你可能感兴趣的:(嵌入式 文件中获取想要字符串初试牛刀sed与awk结合)