Review extension
- 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
- awk merge a file http://ask.apelearn.com/question/493
# awk 'NR==FNR{a[$1]=$2}NR>FNR{print $0,a[$1]}' 1.txt 2.txt
- Explanation: NR represents the number of lines read, FNR represents the current number of lines read
- So in fact, NR==FNR means when reading 2.txt. Similarly, NR>FNR means when reading 1.txt
- Array a is actually equivalent to a map
- Concatenate multiple lines of a file into one line http://ask.apelearn.com/question/266
# awk '{printf("%s ",$0)}' file
- The meanings of %c, %d, %s, and % in C language printf are as follows:
- %c means output a character
- %d means output an integer
- %s means output a string
- % indicates remainder in operation
- The use of gsub function in awk http://ask.apelearn.com/question/200
# awk 'gsub(/www/,"abc")' /awktest/bkpasswd
// Replace all www with abc in passwd file# awk -F ':' 'gsub(/www/,"abc",$1) {print $0}' /awktest/bkpasswd
// replace www in $1 with abc- The sub function only implements the replacement of the first position, and the gsub function implements the global replacement.
- 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
- 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 的实现方式
- 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
- awk print single quotes with print http://ask.apelearn.com/question/1738
awk '{print "This is a '"'"'"$1} filename 解释一下:在awk中使用脱义字符\是起不到作用的,如果想打印特殊字符,只能使用'""' 这样的组合才可以。 这里自左至右为单引号 双引号 双引号 单引号其中两个单引号为一对,两个双引号为一对。想脱义$那就是'"$"' 脱义单引号那就是 '"'"'
- Merge two files http://ask.apelearn.com/question/945
# paste filename1 filename2
- If you want to connect two files with a specified character, you can also use -d to specify
# paste -d '+' a.txt b.txt
- Awk reference tutorial http://www.cnblogs.com/emanlee/p/3327576.html