shell命令之awk、sed和grep

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 50
B 35 75
C 75 78 
D 99 88 76

輸出:
Not all scores are available for B
Not 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 50
B 35 37 75
C 75 78 80
D 99 88 76
輸出:
A : Fail
B : Fail
C : Pass
D : Pass
程序:
a.循環遍歷每一個元素,小於50,輸出Fail
awk '{
    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替換爲thisse

sed 's/thy /your /gi' //替換所有的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
**** **** **** 1232
a.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 8888
sed -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

題目出處:https://www.hackerrank.com/domains/shell/grep-sed-awk/page:1

發佈了342 篇原創文章 · 獲贊 78 · 訪問量 43萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章