linux里gsub命令 linux gsub
linux shell 处理文本
先提取数字,再合并打印两个文件
站在用户的角度思考问题,与客户深入沟通,找到东安网站设计与东安网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、网站建设、企业官网、英文网站、手机端网站、网站推广、申请域名、虚拟主机、企业邮箱。业务覆盖东安地区。
#!/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
在linux下如何用正则表达式执行ifconfig命令,只显示本机IP地址的内容!
记得用root身份执行。
ifconfig eth0 |awk '/inet addr/{gsub(/addr:/,"");print $2}'
参考
excel或linux或Python统计特定字符的数量
Linux下一条shell语句解决问题(假设文件名为file.txt):
sed -r 's/\^[^0-9]+//' file.txt | awk '{print gsub("[ATCG]","")}'
首先使用sed的替换命令s,将 ^ 以及紧跟其后的非数字字符全都去除(替换为空)。
接着使用awk进一步处理,gsub返回替换次数。这里做替换的目的其实不是替换,只为得到ATCG字母出现次数之和。
Linux:关于awk的sub函数,求解,这个为什么和预期结果不一样呢?
-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'
linux shell难吗
不太难,要是你花点时间学, 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 必学的一环,我应为用途很大,你看是不是? :)
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.
希望能帮你解决
网站栏目:linux里gsub命令 linux gsub
网页URL:http://pwwzsj.com/article/doosccc.html