shosei

awk one liners

Feb 23rd, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 1.13 KB | None | 0 0
  1. # File Spacing
  2.  
  3. # double space a file
  4. awk '1;{print ""}'
  5. awk 'BEGIN{ORS="\n\n"};1'
  6.  
  7. # triple space
  8.  awk '1;{print "\n"}'
  9.  
  10.  # precede each line by its line number FOR ALL FILES TOGETHER, with tab.
  11.  awk '{print NR "\t" $0}' files*
  12.  
  13.  # count lines (emulates "wc -l")
  14.  awk 'END{print NR}'
  15.  
  16.  # print every line after replacing each field with its absolute value
  17.  awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'
  18.  awk '{for (i=1; i<=NF; i++) $i = ($i < 0) ? -$i : $i; print }'
  19.  
  20.  # print the total number of lines that contain "Darn"
  21.  awk '/Darn/{n++}; END {print n+0}' file
  22.  
  23.  # print the last field of the last line
  24.  awk '{ field = $NF }; END{ print field }'
  25.  
  26.  # print every line with more than 4 fields
  27.  awk 'NF > 4'
  28.  
  29.  # delete leading whitespace (spaces, tabs) from front of each line
  30.  # aligns all text flush left
  31.  awk '{sub(/^[ \t]+/, "")};1'
  32.  
  33.  # delete trailing whitespace (spaces, tabs) from end of each line
  34.  awk '{sub(/[ \t]+$/, "")};1'
  35.  
  36. # grep for AAA and BBB and CCC (in any order on the same line)
  37.  awk '/AAA/ && /BBB/ && /CCC/'
  38.  
  39.  # grep for AAA and BBB and CCC (in that order)
  40.  awk '/AAA.*BBB.*CCC/'
Advertisement
Add Comment
Please, Sign In to add comment