* $ echo $var| tr -cd g | wc -c2
$ echo -n$var| sed 's/[^g]//g'| wc -c2
$ echo -n$var| sed 's/[^gt]//g'| wc -c5
3.統計單詞的個數
* $ echo$var | wc -w
4.bash提供的數組數據結構,它是以數字為下標的,和C語言從0開始的下一樣
* $ var="get length of me"$ var_arr=($var) #這里把var字符串,以數組的形式存放到var_arr中,默認以空格分割$ echo ${var_arr[0]} ${var_arr[1]} ${var_arr[2]} ${var_arr[3]} ${var_arr[4]}
get the length of me
$ echo ${var_arr[@]} #@表示整個字符串
get the length of me
$ echo ${var_arr[*]} #*表示整個字符串
get the length of me
$ echo ${#var_arr[@]}#求得整個字符數組的元素個數5$ echo ${#var_arr[0]}#求得某個字符串的字符個數3$ var_arr[5]="new_element"#直接給數組元素賦值$ echo ${var_arr[@]}
get the length of me new_element
$ echo ${#var_arr[@]}6
* $ for i in$var; do echo $i" "; done;
get the length of me
* $ var="get length of me"
$ echo${var:0:3}
get
$ echo${var(-2)}#反方向
me
$ echo `expr substr "$var"53`
the
$ echo$var | awk '{printf("%s\n",substr($0,9,6))}'#從第九個位置開始選取6個字符
length
* 使用cut取子串
$ echo$var | cut -d" "-f5
me
* $ echo$var | sed 's/ [a-z]*//g'
get
刪除空格+字母串
$ echo$var | sed 's/[a-z]* //g'
me
刪除字母串+空格
7.匹配求子串
* $ echo ${var%% *} #從最右邊開始,刪除最左邊空格右邊的所有字符
get
* $ echo ${var% *} #從最右邊開始,刪除第一個空格右邊的所有字符
get the length of
* $ echo ${var##* } #從最左邊開始,刪除最右邊空格左邊的所有字符
me
* $ echo ${var#* } #從最左邊開始,刪除第一個空格左邊的所有字符
the length of me
8.sed有按行打印的功能,記得用tr把空格換為行號
* $ echo$var | tr " ""\n" | sed -n 5p
me
* $ echo$var | tr " ""\n" | sed -n 1p
get
* $ var="get the length of me"$ expr index "$var" t
3
* $ echo $var | awk '{printf("%d\n",match($0,"the"));}'5
12.子串替換
* $ var="get the length of me"
$ echo ${var/ /_} #把第一個空格替換成下劃線
get_the length of me
$ echo ${var// /_} #把所有的空格都替換成下劃線
get_the_length_of_me
$ echo$var | awk '{sub(" ","_",$0);printf("%s\n",$0);}'
get_the length of me
$ echo$var | awk '{gsub(" ","_",$0);printf("%s\n",$0);}'
get_the_length_of_me
$ echo$var | sed 's/ /_/'
get_the length of me
$ echo$var | tr " ""_"
get_the_length_of_me
$ echo$var | tr "[a-z]""[A-Z]"
GET THE LENGTH OF ME
13.tac 會將文本的內容倒置顯示,正如名字和 cat 相反一樣,功能和cat也是相反的
14.插入子串
* $ var="get the length of me"
$ echo ${var/ /_ } #在第一個空格前加上_
get_ the length of me
$ echo ${var// /_ } #在所有的空格前都加上_
get_ the_ length_ of_ me
$ echo ${var/ / _}
get _the length of me
$ echo ${var// / _} #在所有的空格之后都加上_
get _the _length _of _me* $ echo$var | sed 's//\1_/'#在第一個空格之后加上_
get _the length of me
$ echo$var | sed 's//\1_/g'
get _the _length _of _me
$ echo$var | sed 's/[a?z]? [a?z]?/\2 \1/'#調換get和the的位置
the get length of me
Problem Description Calculate ∑ni1im mod (10000000007) for given n,m. Input Input contains two integers n,m(1≤n≤1000,0≤m≤10). Output Output the answer in a single line. Example Input 10 0 Example Output 10 方法:快速冪和大數求和
…
一:網絡基礎配置 1. eth0設置不正確,導致無法正常啟動,修改eth0配置文件就好 ubuntu 12.04的網絡設置文件是/etc/network/interfaces,打開文件,會看到 auto lo iface lo inet loopback 這邊的設置是本地回路。在后…
Problem Description 一個整數,只知道前幾位,不知道末二位,被另一個整數除盡了,那么該數的末二位該是什么呢?
Input 輸入數據有若干組,每組數據包含二個整數a,b(0<10000,10<b<100)&…