awk extension exercise

Review extension

  1. Using external shell variables in awk http://ask.apelearn.com/question/199
        说明:-v选项用于定义参数,这里表示将变量A的值赋予GET_A。
    有多少个变量需要赋值,就需要多少个-v选项。与之等价的:应用于脚本中:
    
    #! /bin/bash
    sort -n filename |awk -F ':' '{print $1}'|uniq >id.txt
    for id in `cat id.txt`; do
            echo "[$id]"
            awk -v id2=$id -F ':' '$1==id2 {print $2}' filename  // 另外的方式为: awk -F ':' '$1=="'id'" {print $2}' filename  
    done
    
  2. awk merge a file http://ask.apelearn.com/question/493
    1. # awk 'NR==FNR{a[$1]=$2}NR>FNR{print $0,a[$1]}' 1.txt 2.txt
    2. Explanation: NR represents the number of lines read, FNR represents the current number of lines read
    3. So in fact, NR==FNR means when reading 2.txt. Similarly, NR>FNR means when reading 1.txt
    4. Array a is actually equivalent to a map
  3. Concatenate multiple lines of a file into one line http://ask.apelearn.com/question/266
    1. # awk '{printf("%s ",$0)}' file
    2. The meanings of %c, %d, %s, and % in C language printf are as follows:
      1. %c means output a character
      2. %d means output an integer
      3. %s means output a string
      4. % indicates remainder in operation
  4. The use of gsub function in awk http://ask.apelearn.com/question/200
    1. # awk 'gsub(/www/,"abc")' /awktest/bkpasswd// Replace all www with abc in passwd file
    2. # awk -F ':' 'gsub(/www/,"abc",$1) {print $0}' /awktest/bkpasswd// replace www in $1 with abc
    3. The sub function only implements the replacement of the first position, and the gsub function implements the global replacement.
  5. Awk intercepts the specified multiple domains as one line http://ask.apelearn.com/question/224
    for j in `seq 0 20`; do
        let x=100*$j
        let y=$x+1
        let z=$x+100
        for i in `seq $y $z` ; do
                awk  -v a=$i '{printf $a " "}' example.txt >>/tmp/test.txt
                echo " " >>/tmp/test.txt
            done
    done
    
  6. Filter two or more keywords http://ask.apelearn.com/question/198
    grep -E '123|abc' filename  // 找出文件(filename)中包含123或者包含abc的行
    egrep '123|abc' filename    //用egrep同样可以实现
    awk '/123|abc/'  filename // awk 的实现方式
    
  7. Generate the following structure file with awk http://ask.apelearn.com/question/5494
    用awk编写生成以下结构文件的程序。( 最后列使用现在的时间,时间格式为YYYYMMDDHHMISS)  各列的值应如下所示,每增加一行便加1,共500万行。
    
    1,1,0000000001,0000000001,0000000001,0000000001,0000000001,0000000001,2005100110101
    2,2,0000000002,0000000002,0000000002,0000000002,0000000002,0000000002,2005100110101
    
    #! /bin/bash
    
    for i in `seq 1 5000000`; do
        n=`echo "$i"|awk '{print length($0)}'`
        export m=$[10-$n]
        export o=`perl -e '$a='0'; $b=$a x $ENV{"m"}; print $b;'`
        export j=$i
        p=`perl -e '$c=$ENV{"o"}.$ENV{"j"}; print $c;'`
        echo "$i,$i,$p,$p,$p,$p,$p,$p,`date +%Y%m%d%H%M%S`"
    done
    
  8. awk print single quotes with print http://ask.apelearn.com/question/1738
    awk '{print "This is a '"'"'"$1} filename 
    解释一下:在awk中使用脱义字符\是起不到作用的,如果想打印特殊字符,只能使用'""' 这样的组合才可以。
    这里自左至右为单引号 双引号 双引号 单引号其中两个单引号为一对,两个双引号为一对。想脱义$那就是'"$"' 脱义单引号那就是 '"'"'
    
  9. Merge two files http://ask.apelearn.com/question/945
    1. # paste filename1 filename2
    2. If you want to connect two files with a specified character, you can also use -d to specify
      # paste -d '+' a.txt b.txt
  10. Awk reference tutorial http://www.cnblogs.com/emanlee/p/3327576.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325224907&siteId=291194637
awk