首页 > 学院 > 开发设计 > 正文

shell命令学习总结(综合版)

2019-11-08 18:24:01
字体:
来源:转载
供稿:网友

shell基本命令for循环数学计算逻辑表达式数学表达式的计算数组的基本操作文本操作cut命令head命令tr命令sort命令uniq命令paste命令awk和sed命令使用awk命令sed命令grep命令

1.shell基本命令

for循环

for循环输出1,3,5,7,9…97,99

for((i=1;i<100;i+=2))do echo $i;done//更喜欢这种for循环

数学计算

计算从控制台读取的两个数的和、差、积和商 算术运算有三种方式:使用 exPR 外部程式、使用[]、使用(( ))和let表达式,个人更喜欢$(( )).

read xread yecho $((x + y))echo $((x - y))echo $((x * y))echo $((x / y))

逻辑表达式

if else条件判断表达式,比较从控制台输入的两个整数的大小

read xread yif [ $x -gt $y ]; thenecho "X is greater than Y"elif [ $x -lt $y ]; thenecho "X is less than Y"elseecho "X is equal to Y"fi//要注意在'['后面和']'前面都必须要有空格;

判断字符串的是否相等

read chif [ $ch = 'Y' -o $ch = 'y' ]; thenecho "YES"elseecho "NO"fi//字符串判断用"="判断两个字符串是否相等,而不是"==";

判断三角形的形状:等边、等腰或不等边

read aread bread cif [ $a = $b -a $a = $c -a $b = $c ]; thenecho "EQUILATERAL"elif [ $a = $b -o $a = $c -o $b = $c ]; thenecho "ISOSCELES"elseecho "SCALENE"fi//使用-o或-a来连接不同的逻辑判断条件

字符串判断 [ -z str ] 如果str的长度为零则返回为真,即空是真 [ str ]  如果字符串不为空则返回为真,与-n类似 [ str1 = str2 ] 如果两个字符串相同则返回为真 [ str1 != str2 ] 如果字符串不相同则返回为真 [ str1 < str2 ] 如果 str1字典排序在str2前面则返回为真。

数值判断 [ num1 -eq num2 ] 等于 [ num1 -ne num2 ] 不等 [ num1 -gt num2 ] 大于 [ num1 -ge num2 ] 大于等于 [ num1 -lt num2 ] 小于 [ num1 -le num2 ] 小于等于

逻辑判断 [ ! EXPR ] 逻辑非 [ EXPR1 -a EXPR2 ] 逻辑与 [ EXPR1 -o EXPR2 ] 逻辑或 [ ] || [ ] 用OR来合并两个条件 [ ] && [ ] 用AND来合并两个条件

数学表达式的计算

计算输入的数学表达式的值

read str //从控制台读取数学表达式printf "%.3f" $(echo $str | bc -l) //计算数学表达式 //"%.3f"输出浮点数,在小数点第三位进行四舍五入//echo $str | bc -l 计算数学表达式,如echo "5/3" | bc -l,输出1.66666666666666666666

从控制台读取一定数目的数字,然后计算器平均值

read countsum=0for((i=0;i<count;i++))doread numsum=$((sum+num))doneprintf "%.3f" $(echo "$sum/$count" | bc -l)

count用来表示读取数字的数量,sum表示和,然后通过echo “sum/count” | bc -l计算其平局值

2.数组的基本操作

1.将多行文本合并为一行

i=0while read linedoarr[$i]=$line((i++))doneecho ${arr[@]} //echo ${arr[@]}输出所有的数组元素**Input**NamibiaNauruNepalNetherlandsNewZealandNicaraguaNigerNigeriaNorthKoreaNorway**Output**Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway

2.将包含‘a’字母的文本行删掉

i=0while read linedoarr[$i]=$line((i++))doneecho ${arr[@]/*[aA]*/}**Input**NamibiaNauruNepalNetherlandsNewZealandNicaraguaNigerNigeriaNorthKoreaNorway**Output**Niger**Output2**echo ${arr[@]/*[aA]*/hello} //使用hello替换掉所有包含a的文本行hello hello hello hello hello hello Niger hello hello hello

3.将上述输入文本重复输出三次

X=$(paste -sd' ' fileName)echo $X $X $XX=$(cat fileName)echo $X $X $X**Output**Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway

4.输出某个元素

echo ${arr[3]}

5.统计输入文本有多少行

a.使用wc命令wc -lb.使用echo命令arr=($(cat))echo ${#arr[@]}c.使用for循环i=0while read linedoarr[$i]=$line((i++))doneecho "$i"

6.将每行第一个大写字母替换为.

a.使用sed命令仅替换第一个大写字母为.sed 's/[A-Z]/./' | paste -sd ' 'b.使用数组的替换来实现X=($(cat)) echo "${X[@]/[A-Z]/.}"**Output**.amibia .auru .epal .etherlands .ewZealand .icaragua .iger .igeria .orthKorea .orway

7.找出一组数据中落单的数

a.首先将' '替换为换行,然后对每行数据排序,获取只出现一次的数字tr ' ' '/n' | sort | uniq -u

3.文本操作

cut命令

获取每行的第三个字母

cut -c3 fileName

获取每行的第三至第五个字母

cut -c3-5 fileName

获取每行的第三和第五个字母

cut -c3,5 fileName

获取每行的前三个字母

cut -c-3 fileName

获取每行的第五个字母往后的字母

cut -c5- fileName

获取文本中的前三个字段

cut -f1-3 fileName //默认的文本分隔符为/t//输入1 New York, New York[10] 8,244,910 1 New York-Northern New Jersey-Long Island, NY-NJ-PA MSA 19,015,900 1 New York-Newark-Bridgeport, NY-NJ-CT-PA CSA 22,214,0832 Los Angeles, California 3,819,702 2 Los Angeles-Long Beach-Santa Ana, CA MSA 12,944,801 2 Los Angeles-Long Beach-Riverside, CA CSA 18,081,5693 Chicago, Illinois 2,707,120 3 Chicago-Joliet-Naperville, IL-IN-WI MSA 9,504,753 3 Chicago-Naperville-Michigan City, IL-IN-WI CSA 9,729,8254 Houston, Texas 2,145,146 4 Dallas-Fort Worth-Arlington, TX MSA 6,526,548 4 Washington-Baltimore-Northern Virginia, DC-MD-VA-WV CSA 8,718,0835 Philadelphia, Pennsylvania[11] 1,536,471 5 Houston-Sugar Land-Baytown, TX MSA 6,086,538 5 Boston-Worcester-Manchester, MA-RI-NH CSA 7,601,061//输出1 New York, New York[10] 8,244,9102 Los Angeles, California 3,819,7023 Chicago, Illinois 2,707,1204 Houston, Texas 2,145,1465 Philadelphia, Pennsylvania[11] 1,536,471

获取输入文本的第四个字段

cut -d' ' -f4 fileName**input**HelloWorldhow are you**output**HelloWorld//输入的三行最大字段都不超过4个,为什么只输出Hello和World呢?这题不太靠谱

输出某个字段或字段范围和输出某个字母或字符范围类似,不再重复

head命令

输出前20行

head -n20 fileName //不存在20行的话有多少行算多少行

输出第11到第20行

head -n20 fileName | tail -n10

输出前20个字符

head -c20 fileName //不存在20个字符的话有多少算多少

输出后20行

tail -n20 fileName //不存在20行的话有多少行算多少行

输出后20个字符

tail -c20 fileName //不存在20个字符的话有多少算多少

tr命令

将文本中的一些字母按先后顺序替换为另外一些字母

tr '()' '[]' fileName**Input**int i=(int)5.8(23 + 5)*2**Output**int i=[int]5.8[23 + 5]*2tr '[a-z]' '[A-Z]' fileName //将文本中的小写字母替换为大写字母

删除文本中存在的小写字母

tr -d '[a-z]' fileName**Input**HelloWorldhow are you**Output**HW

将多个连续出现的给定字符合并为一个

tr -s ' ' fileName //合并多个空格为一个**Input**He lloWor ldhow are you**Output**He lloWor ldhow are you

sort命令

对文本行进行排序

sort fileName**Input**Dr. Rajendra Prasad January 26, 1950 May 13, 1962Dr. S. Radhakrishnan May 13, 1962 May 13, 1967Dr. Zakir Hussain May 13, 1967 August 24, 1969Shri Varahagiri Venkata Giri August 24, 1969 August 24, 1974Shri Fakhruddin Ali Ahmed August 24, 1974 February 11, 1977Shri Neelam Sanjiva Reddy July 25, 1977 July 25, 198**Output**Dr. Rajendra Prasad January 26, 1950 May 13, 1962Dr. S. Radhakrishnan May 13, 1962 May 13, 1967Dr. Zakir Hussain May 13, 1967 August 24, 1969Shri Fakhruddin Ali Ahmed August 24, 1974 February 11, 1977Shri Neelam Sanjiva Reddy July 25, 1977 July 25, 198Shri Varahagiri Venkata Giri August 24, 1969 August 24, 1974sort -r fileName //降序排序

对数字进行排序 sort -rn fileName //对数字进行降序排序 -n对数字排序

对输入文本中第二个字段数字进行降序排序

sort -t$'/t' -nr -k2 fileName//-t$'/t' 以tab分割文本//-nr 数字降序排序//-k2 第二个字段**Input**Albany, N.Y. 22.2 46.6 71.1 49.3 38.60 136 64.4 57Albuquerque, N.M. 35.7 55.6 78.5 57.3 9.47 60 11.0 64Anchorage, Alaska 15.8 36.3 58.4 34.1 16.08 115 70.8 39 / 60Asheville, N.C. 35.8 54.1 73.0 55.2 47.07 126 15.3 39Atlanta, Ga. 42.7 61.6 80.0 62.8 50.20 115 2.1 69 / 65Atlantic City, N.J. 32.1 50.6 75.3 55.1 40.59 113 16.2 60 / 54Austin, Texas 50.2 68.3 84.2 70.6 33.65 85 0.9 62 / 58Baltimore, Md. 32.3 53.2 76.5 55.4 41.94 115 21.5 53Baton Rouge, La. 50.1 66.6 81.7 68.1 63.08 110 0.2 52 / 46Billings, Mont. 24.0 46.1 72.0 48.1 14.77 96 56.9 69Birmingham, Ala. 42.6 61.3 80.2 62.9 53.99 117 1.5 60Bismarck, N.D. 10.2 43.3 70.4 45.2 16.84 96 44.3 64Boise, Idaho 30.2 50.6 74.7 52.8 12.19 89 20.6 64Boston, Mass. 29.3 48.3 73.9 54.1 42.53 127 42.8 52 / 66Bridgeport, Conn. 29.9 48.9 74.0 54.7 44.15 119 26.2 55 / 49**Output**Austin, Texas 50.2 68.3 84.2 70.6 33.65 85 0.9 62 / 58Baton Rouge, La. 50.1 66.6 81.7 68.1 63.08 110 0.2 52 / 46Atlanta, Ga. 42.7 61.6 80.0 62.8 50.20 115 2.1 69 / 65Birmingham, Ala. 42.6 61.3 80.2 62.9 53.99 117 1.5 60Asheville, N.C. 35.8 54.1 73.0 55.2 47.07 126 15.3 39Albuquerque, N.M. 35.7 55.6 78.5 57.3 9.47 60 11.0 64Baltimore, Md. 32.3 53.2 76.5 55.4 41.94 115 21.5 53Atlantic City, N.J. 32.1 50.6 75.3 55.1 40.59 113 16.2 60 / 54Boise, Idaho 30.2 50.6 74.7 52.8 12.19 89 20.6 64Bridgeport, Conn. 29.9 48.9 74.0 54.7 44.15 119 26.2 55 / 49Boston, Mass. 29.3 48.3 73.9 54.1 42.53 127 42.8 52 / 66Billings, Mont. 24.0 46.1 72.0 48.1 14.77 96 56.9 69Albany, N.Y. 22.2 46.6 71.1 49.3 38.60 136 64.4 57Anchorage, Alaska 15.8 36.3 58.4 34.1 16.08 115 70.8 39 / 60Bismarck, N.D. 10.2 43.3 70.4 45.2 16.84 96 44.3 64sort -r fileName //降序排序

uniq命令

uniq fieName//如果相邻行文本重复,只保留一行**Input**00000101000202**Output**00010002 uniq -u fileName fieName//获取行中与前后行都不同的行文本**Output**00uniq -d fileName fieName//获取文本中前后相同的行文本**Output**000102uniq -c | cut -c7- fieName//获取文本中的字段统计**Output**2 002 011 002 02uniq -ci | cut -c7- fieName//获取文本中的字段统计,不区分大小写如输入**Input**aAAaAAaa**output**4 aA

paste命令

paste -d':' fileName1 fileName2 //将两个文件按行合并,以':'分割paste -s fileName //将文本以'/t'为分隔符进行合并**Input**Albany, N.Y.Albuquerque, N.M.Anchorage, AlaskaAsheville, N.C.Atlanta, Ga.Atlantic City, N.J.Austin, TexasBaltimore, Md.Baton Rouge, La.Billings, Mont.Birmingham, Ala.Bismarck, N.D.Boise, IdahoBoston, Mass.Bridgeport, Conn.**Output**Albany, N.Y. Albuquerque, N.M. Anchorage, Alaska Asheville, N.C.Atlanta, Ga. Atlantic City, N.J. Austin, Texas Baltimore, Md. Baton Rouge, La. Billings, Mont. Birmingham, Ala. Bismarck, N.D. Boise, Idaho Boston, Mass. Bridgeport, Conn.paste - - - -d$'/t' fileName //将文本以'/t'为分隔符进行合并,每行三列**Input**Albany, N.Y.Albuquerque, N.M.Anchorage, AlaskaAsheville, N.C.Atlanta, Ga.Atlantic City, N.J.Austin, TexasBaltimore, Md.Baton Rouge, La.Billings, Mont.Birmingham, Ala.Bismarck, N.D.Boise, IdahoBoston, Mass.Bridgeport, Conn.**Output**Albany, N.Y. Albuquerque, N.M. Anchorage, AlaskaAsheville, N.C. Atlanta, Ga. Atlantic City, N.J.Austin, Texas Baltimore, Md. Baton Rouge, La.Billings, Mont. Birmingham, Ala. Bismarck, N.D.Boise, Idaho Boston, Mass. Bridgeport, Conn.paste -d';' -s fileName //将文本合并为一行,用';'分割**Output**Albany, N.Y.;Albuquerque, N.M.;Anchorage, Alaska;Asheville, N.C.;Atlanta, Ga.;Atlantic City, N.J.;Austin, Texas;Baltimore, Md.;Baton Rouge, La.;Billings, Mont.;Birmingham, Ala.;Bismarck, N.D.;Boise, Idaho;Boston, Mass.;Bridgeport, Conn.paste - - - -d';' //将文本合并为一行,用';'分割,每行三列**Output**Buffalo, N.Y.;Burlington, Vt.;Caribou, MaineCasper, Wyo.;Charleston, S.C.;Charleston, W.Va.

4.awk和sed命令使用

awk命令

1.输出字段不全的文本行

a.通过第四列是否为空来判断awk '{ if( $4 == "") { print "Not all scores are available for "$1 }}'b.通过列数是否小于4来判断awk '{ if( NF < 4) { print "Not all scores are available for "$1 }}'输入:A 25 27 50B 35 75C 75 78 D 99 88 76输出:Not all scores are available for BNot all scores are available for C

2.判断一个学生的成绩是否几及格

描述:A student is considered to have passed if (s)he has a score or more in each of the three subjects.输入:A 25 27 50B 35 37 75C 75 78 80D 99 88 76输出:A : FailB : FailC : PassD : Pass程序:a.循环遍历每一个元素,小于50,输出Failawk '{ flag=1; for(i=2;i<NF;i++) { if($i < 50) { print $1" : Fail"; flag=0; break; } } if(flag==1) { print $1" : Pass"; }}'b.几乎相同的方法,awk对每一行的文本进行处理awk '{ if( $1 < 50 || $2 < 50 || $3 < 50) { print $1" : Fail" } else { print $1" : Pass" }}'

3.统计平均分,根据平均分输出A、B、C或Fail 描述:Your task is to identify the performance grade for each student. If the average of the three scores is 80 or more, the grade is ‘A’. If the average is 60 or above, but less than 80, the grade is ‘B’. If the average is 50 or above, but less than 60, the grade is ‘C’. Otherwise the grade is ‘FAIL’. 输入: A 25 27 50 B 35 37 75 C 75 78 80 D 99 88 76 输出: A 25 27 50 : FAIL B 35 37 75 : FAIL C 75 78 80 : B D 99 88 76 : A 代码:

//a.对每一行做处理awk '{ avg = ($4 + $2 + $3)/3; printf $0;printf" : "; if(avg < 50) print "FAIL"; else if(avg < 60) print "C"; else if(avg < 80) print "B"; else print "A";}'//$0表示整行,$1表示第一列,$2表示第二列

4.格式化输出文本行 输入: A 25 27 50 B 35 37 75 C 75 78 80 D 99 88 76 输出: A 25 27 50;B 35 37 75 C 75 78 80;D 99 88 76 代码:

a.使用awk偶数行输出文本行回车,奇数行输出文本和';'awk '{ if( NR%2 == 0 ) { printf $0"/n"; } else { printf $0";"; }}'b.使用paste命令来实现paste - - -d';' fileName

sed命令

1.将文本中首次出现的the替换为this

sed s/"the "/"this "/ fileName //"the "是因为防止将these替换为thissesed 's/thy /your /gi' fileName//替换所有的thy,不区分大小写

2.将thy不区分大小写地加{}强调表示

sed 's/thy/{&}/gi' fileName //{&} 加{}且输出其本身

3.文本替换

将部分文本替换为****输入:1234 5678 9101 1234 2999 5178 9101 2234 9999 5628 9201 1232 8888 3678 9101 1232输出:**** **** **** 1234**** **** **** 2234**** **** **** 1232**** **** **** 1232a.sed -r -e 's/[0-9]{4} /**** /g' fileName 将四个数字和一个空格组成的子串替换为"**** "

4.将部分子串颠倒位置

输入:1234 5678 9101 1234 2999 5178 9101 2234 9999 5628 9201 1232 8888 3678 9101 1232输出:1234 9101 5678 1234 2234 9101 5178 2999 1232 9201 5628 9999 1232 9101 3678 8888sed -r 's/(.+ )(.+ )(.+ )(....)//4 /3/2/1/' fileName

grep命令

1.使用grep获取匹配整个单词的某行

grep -w "the" fileName //-w 匹配整个单词grep -iw "the" fileName //不区分大小写grep -ivw 'that' fileName //-v

2.匹配文本中包含the 、that 、then 和those的文本

grep -iw -e 'th[e,at,en,ose]' fileName或者grep -iw -e 'the' -e 'that' -e 'then' -e 'those' fileName

题目出处:hackerrank.com


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表