重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
先提取數字,再合并打印兩個文件
站在用戶的角度思考問題,與客戶深入溝通,找到東安網站設計與東安網站推廣的解決方案,憑借多年的經驗,讓設計與互聯網技術結合,創造個性化、用戶體驗好的作品,建站類型包括:成都網站設計、網站建設、企業官網、英文網站、手機端網站、網站推廣、申請域名、虛擬主機、企業郵箱。業務覆蓋東安地區。
#!/bin/sh
cat a.txt|sed -re 's/[^0-9]*([0-9]*).*$/\1/;' b.txt
awk '{if(NR==FNR){a[FNR]=$1;}else{print $1,a[FNR]}}' b.txt a.txt
記得用root身份執行。
ifconfig eth0 |awk '/inet addr/{gsub(/addr:/,"");print $2}'
參考
Linux下一條shell語句解決問題(假設文件名為file.txt):
sed?-r?'s/\^[^0-9]+//'?file.txt?|?awk?'{print?gsub("[ATCG]","")}'
首先使用sed的替換命令s,將 ^ 以及緊跟其后的非數字字符全都去除(替換為空)。
接著使用awk進一步處理,gsub返回替換次數。這里做替換的目的其實不是替換,只為得到ATCG字母出現次數之和。
-F是指定域分隔符,默認情況下的域分隔符是:空格、制表符(tab),將一行分為一個個字段,你這里將分隔符指定為, ,很明顯就沒有第二個字段了,也就是$2是空的。
man awk 可知
sub(r, s [, t]) ? ? ? ? Just like gsub(), but only the first matching substring is replaced.
是指在對應字段中,僅僅將第一次匹配到的字符串進行替換。對應的還有gsub函數,在字段中替換全部匹配到的字符串。
你這里作用于$2,而$2本身就是空的,自然就沒變化了。你明白了么?
awk?'{sub(/test/,"TEST",$2)}1'
不太難,要是你花點時間學, shell 編程對於系統管理員或是一般使用者
都相當有用,除了自動化,還可寫出一些有趣的小工具, 例如
我為自己寫了個代替fortune 的小玩意,在 terminal 啟動時執行這腳本,
它會連上 randomfunfacts.com, 隨機地提供一些有趣的事實,和 vista
一個 gedget 一樣,我在 .bash_profile 加入
# have some fun
if [ -x $(which funfacts) ]
then
funfacts | tee /etc/motd
else
fortune -s | tee /etc/motd
fi
而腳本放在 $HOME/bin
該腳本很簡單,如是
#! /bin/bash
# get random fun facts from randomfunfacts.com, like `fortune'does.
# $prog: funfacts twfccc@gmail.com, twf_cc@yahoo.com.hk
# $required: lynx or links needed, gnu grep 2.5.X and bash 3.X or above
url="randomfunfacts.com" # the website
browser=lynx # default text browser
if which links /dev/null
then
browser=links # better choice if system provide
fi
if ! which lynx /dev/null ! which links /dev/null
then
echo "lynx or links not found, sorry."
echo "Install one of them and execute script again."
exit 5
fi
case "$browser" in
links) facts="-dump $url | sed '/+-/,/+-/!d'" ;;
lynx) facts="-dump $url | grep -A3 'U' | sed 1D" ;;
esac
eval $browser $facts
exit 0
執行是這樣
User@User-PC ~
$ funfacts
+------------------------------------------------------------------------------+
| An earthquake on Dec. 16, 1811 caused parts of the Mississippi River to flow |
| backwards! |
+------------------------------------------------------------------------------+
User@User-PC ~
$ funfacts
+------------------------------------------------------------------------------+
| An ostrich's eye is bigger than its brain. |
+------------------------------------------------------------------------------+
User@User-PC ~
$ !!
funfacts
+------------------------------------------------------------------------------+
| A crocodile cannot stick its tongue out. |
+------------------------------------------------------------------------------+
User@User-PC ~
$
也可用來寫些實用工具,如到網站找最新代理服務器
$ cat bin/getproxy.sh
#! /bin/bash
# get update proxy list from
# need gnu sed 4.1.X or above , bash 3.2.X or above
remotehost=""
port=80
header="GET HTTP/1.0\n\n
Connection: Keep-Alive\n\n
Accept: text/html\n\n
Accept-Charest: iso-8859-1, *, utf-8\n\n
Accept-Language: en, zh, ja\n\n
Host: \n\n
User-Agent: bash_script/0.1 [en, zh, ja] (Cygwin, NT)\n\n"
exec 5 /dev/tcp/$remotehost/$port
printf "$header\n\n" 5
sed -rn '/^td[0-9\.]+\/td$/p;
/^td[0-9]+\/td$/p;
/^td[a-z]+ ?[a-z]+\/td$/p;
/^td[A-Z][a-z]+ ?[a-zA-Z]+\/td$/p;
/^td20[0-1][0-9]-0?[1-9]+-0?[0-9]+\/td$/p' 5 |
sed -r 's/^td(Name|IP|Port|Country|Type|Last Test)\/td$//g' |
sed '/^$/d' |
sed -r 's/[^]+//g;/20[0-1][0-9]-0?[1-9]+-0?[0-9]+/s//\n----------------\n/g'
exec 5- 5-
awk 版本
User@User-PC ~
$ cat bin/getproxy.awk
#!/usr/bin/gawk -f
# get update proxy list from
BEGIN{
header="GET HTTP/1.0\r\n\r\n \
Connection: Keep-Alive\n\r\n\r \
Accept: text/html\n\r\n\r \
Accept-Charest: iso-8859-1, *, utf-8\n\r\n\r \
Accept-Language: en, zh, ja\n\r\n\r \
Host: \n\r\n\r \
User-Agent: gawk_script/0.99 [en, zh] (Cygwin, NT)\n\r\n\r"
host=""
remotehost="/inet/tcp/0/" host "/80"
print header | remotehost
while ((remotehost | getline) 0){
sub(/^td(IP|Port|Country|Type|Last Test)\/td$/, "")
if ($0 ~ /^td[0-9\.]+\/td$/){
gsub(/[^]+/, "")
print $0
} else if ($0 ~ /^td[0-9]+\/td$/){
gsub(/[^]+/, "")
print $0
} else if ($0 ~ /^td[a-z]+ ?[a-z]+\/td$/){
gsub(/[^]+/, "")
print $0
} else if ($0 ~ /^td[A-Z][a-z]+ ?[a-zA-Z]+\/td$/){
gsub(/[^]+/, "")
print $0
} else if ($0 ~ /^td20[0-1][0-9]-0?[1-9]+-0?[0-9]+\/td$/){
gsub(/[^]+/, "")
print $0
printf("\n----------------\n")
} else {
continue
}
}
close(remotehost)
}
shell 是用Linux 必學的一環,我應為用途很大,你看是不是? :)
1.先將你上面的文本保存為文件 ts.txt
2.保存如下腳本為ts.sh并修改可執行權限。
#!/bin/bash
cat ts.txt |gawk '/IOPATH/ {if (gsub(/\(|\)/,"")) {
t1=$1;t2=$2;t3=$3;
split($4,value1,":");v1=value1[1];v2=value1[2];v3=value1[3];
split($5,value2,":");v11=value2[1];v22=value2[2];v33=value2[3];
print "\t("t1" " t2" " t3 " ("v1*10":"v2*10":"v3*10") ("v11*10":"v22*10":"v33*10"))"};
next} {print}'
3. # ./ts.sh ts1.txt
4. # more ts1.txt 輸出如下,按照你的要求每個數*10
(CELL
(CELLTYPE "OR2X1")
(INSTANCE U2604)
(DELAY
(ABSOLUTE
(IOPATH A Y (6.5:6.5:6.5) (4.57:4.57:4.57))
(IOPATH B Y (6.08:6.08:6.08) (6.82:6.82:6.82))
)
)
)
(CELL
(CELLTYPE "OAI21XL")
(INSTANCE U2603)
(DELAY
(ABSOLUTE
(IOPATH A0 Y (1.11:1.11:1.11) (0.49:0.49:0.49))
(IOPATH A1 Y (3.11:3.11:3.11) (-0.37:-0.37:-0.37))
(IOPATH B0 Y (1.86:1.88:1.88) (0.62:0.62:0.62))
)
)
)
5. # diff ts.txt ts1.txt 比較結果輸出如下
6,7c6,7
(IOPATH A Y (0.650:0.650:0.650) (0.457:0.457:0.457))
(IOPATH B Y (0.608:0.608:0.608) (0.682:0.682:0.682))
---
(IOPATH A Y (6.5:6.5:6.5) (4.57:4.57:4.57))
(IOPATH B Y (6.08:6.08:6.08) (6.82:6.82:6.82))
16,18c16,18
(IOPATH A0 Y (0.111:0.111:0.111) (0.049:0.049:0.049))
(IOPATH A1 Y (0.311:0.311:0.311) (-0.037:-0.037:-0.037))
(IOPATH B0 Y (0.186:0.188:0.188) (0.062:0.062:0.062))
---
(IOPATH A0 Y (1.11:1.11:1.11) (0.49:0.49:0.49))
(IOPATH A1 Y (3.11:3.11:3.11) (-0.37:-0.37:-0.37))
(IOPATH B0 Y (1.86:1.88:1.88) (0.62:0.62:0.62))
從以上比較的結果可以看出每個符合要求的值都乘以了10.
希望能幫你解決