shosei

Awk

Feb 23rd, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 4.28 KB | None | 0 0
  1. # Print first two fields in opposite order:
  2.   awk '{ print $2, $1 }' file
  3.  
  4.  
  5. # Print lines longer than 72 characters:
  6.   awk 'length > 72' file
  7.    
  8.  
  9. # Print length of string in 2nd column
  10.   awk '{print length($2)}' file
  11.  
  12.  
  13. # Add up first column, print sum and average:
  14.        { s += $1 }
  15.   END  { print "sum is", s, " average is", s/NR }
  16.  
  17.  
  18. # Print fields in reverse order:
  19.   awk '{ for (i = NF; i > 0; --i) print $i }' file
  20.  
  21.  
  22. # Print the last line
  23.       {line = $0}
  24.   END {print line}
  25.  
  26.  
  27. # Print the total number of lines that contain the word Pat
  28.   /Pat/ {nlines = nlines + 1}
  29.   END {print nlines}
  30.  
  31.  
  32. # Print all lines between start/stop pairs:
  33.   awk '/start/, /stop/' file
  34.  
  35.  
  36. # Print all lines whose first field is different from previous one:
  37.   awk '$1 != prev { print; prev = $1 }' file
  38.  
  39.  
  40. # Print column 3 if column 1 > column 2:
  41.   awk '$1 > $2 {print $3}' file
  42.      
  43.  
  44. # Print line if column 3 > column 2:
  45.   awk '$3 > $2' file
  46.  
  47.  
  48. # Count number of lines where col 3 > col 1
  49.   awk '$3 > $1 {print i + "1"; i++}' file
  50.  
  51.  
  52. # Print sequence number and then column 1 of file:
  53.   awk '{print NR, $1}' file
  54.  
  55.  
  56. # Print every line after erasing the 2nd field
  57.   awk '{$2 = ""; print}' file
  58.  
  59.  
  60. # Print hi 28 times
  61.   yes | head -28 | awk '{ print "hi" }'
  62.  
  63.  
  64. # Print hi.0010 to hi.0099 (NOTE IRAF USERS!)
  65.   yes | head -90 | awk '{printf("hi00%2.0f \n", NR+9)}'
  66.  
  67. # Print out 4 random numbers between 0 and 1
  68. yes | head -4 | awk '{print rand()}'
  69.  
  70. # Print out 40 random integers modulo 5
  71. yes | head -40 | awk '{print int(100*rand()) % 5}'
  72.  
  73.  
  74. # Replace every field by its absolute value
  75.   { for (i = 1; i <= NF; i=i+1) if ($i < 0) $i = -$i print}
  76.  
  77. # If you have another character that delimits fields, use the -F option
  78. # For example, to print out the phone number for Jones in the following file,
  79. # 000902|Beavis|Theodore|333-242-2222|149092
  80. # 000901|Jones|Bill|532-382-0342|234023
  81. # ...
  82. # type
  83.   awk -F"|" '$2=="Jones"{print $4}' filename
  84.  
  85.  
  86.  
  87. # Some looping commands
  88. # Remove a bunch of print jobs from the queue
  89.   BEGIN{
  90.     for (i=875;i>833;i--){
  91.         printf "lprm -Plw %d\n", i
  92.     } exit
  93.        }
  94.  
  95.  
  96.  Formatted printouts are of the form printf( "format\n", value1, value2, ... valueN)
  97.         e.g. printf("howdy %-8s What it is bro. %.2f\n", $1, $2*$3)
  98.     %s = string
  99.     %-8s = 8 character string left justified
  100.     %.2f = number with 2 places after .
  101.     %6.2f = field 6 chars with 2 chars after .
  102.     \n is newline
  103.     \t is a tab
  104.  
  105.  
  106. # Print frequency histogram of column of numbers
  107. $2 <= 0.1 {na=na+1}
  108. ($2 > 0.1) && ($2 <= 0.2) {nb = nb+1}
  109. ($2 > 0.2) && ($2 <= 0.3) {nc = nc+1}
  110. ($2 > 0.3) && ($2 <= 0.4) {nd = nd+1}
  111. ($2 > 0.4) && ($2 <= 0.5) {ne = ne+1}
  112. ($2 > 0.5) && ($2 <= 0.6) {nf = nf+1}
  113. ($2 > 0.6) && ($2 <= 0.7) {ng = ng+1}
  114. ($2 > 0.7) && ($2 <= 0.8) {nh = nh+1}
  115. ($2 > 0.8) && ($2 <= 0.9) {ni = ni+1}
  116. ($2 > 0.9) {nj = nj+1}
  117. END {print na, nb, nc, nd, ne, nf, ng, nh, ni, nj, NR}
  118.  
  119.  
  120. # Find maximum and minimum values present in column 1
  121. NR == 1 {m=$1 ; p=$1}
  122. $1 >= m {m = $1}
  123. $1 <= p {p = $1}
  124. END { print "Max = " m, "   Min = " p }
  125.  
  126. # Example of defining variables, multiple commands on one line
  127. NR == 1 {prev=$4; preva = $1; prevb = $2; n=0; sum=0}
  128. $4 != prev {print preva, prevb, prev, sum/n; n=0; sum=0; prev = $4; preva = $1; prevb = $2}
  129. $4 == prev {n++; sum=sum+$5/$6}
  130. END {print preva, prevb, prev, sum/n}
  131.  
  132. # Example of defining and using a function, inserting values into an array
  133. # and doing integer arithmetic mod(n). This script finds the number of days
  134. # elapsed since Jan 1, 1901. (from http://www.netlib.org/research/awkbookcode/ch3)
  135. function daynum(y, m, d,    days, i, n)
  136. {   # 1 == Jan 1, 1901
  137.     split("31 28 31 30 31 30 31 31 30 31 30 31", days)
  138.     # 365 days a year, plus one for each leap year
  139.     n = (y-1901) * 365 + int((y-1901)/4)
  140.     if (y % 4 == 0) # leap year from 1901 to 2099
  141.         days[2]++
  142.     for (i = 1; i < m; i++)
  143.         n += days[i]
  144.     return n + d
  145. }
  146.     { print daynum($1, $2, $3) }
  147.  
  148. # Example of using substrings
  149. # substr($2,9,7) picks out characters 9 thru 15 of column 2
  150. {print "imarith", substr($2,1,7) " - " $3, "out."substr($2,5,3)}
  151. {print "imarith", substr($2,9,7) " - " $3, "out."substr($2,13,3)}
  152. {print "imarith", substr($2,17,7) " - " $3, "out."substr($2,21,3)}
  153. {print "imarith", substr($2,25,7) " - " $3, "out."substr($2,29,3)}
Advertisement
Add Comment
Please, Sign In to add comment