Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 1.65 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Passing a Bash Array Element to an Awk Regex Expression
  2. awk '$2 ~ /^/var$/ { print $1 }' /etc/fstab
  3.        
  4. PARTITIONS=(/usr /home /var tmp);
  5. for ((n=0; n<${#PARTITION[@]}; n++)); do
  6.     cat /etc/fstab | awk '$2 ~ /^${PARTITIONS[$n]}$/ { print $1 }';
  7. done
  8.        
  9. PARTITIONS=(/usr /home /var tmp);
  10. for ((n=0; n<${#PARTITION[@]}; n++)); do
  11.     cat /etc/fstab | awk -v partition="${PARTITIONS[$n]}" '$2 ~ /^/var$/ { print $1," ",partition }';
  12. done
  13.        
  14. cat /etc/fstab | awk -v partition="${PARTITIONS[$n]}" '$2 ~ partition { print $1 }'
  15.        
  16. awk -v partition="${partitions[$n]}" '$2 ~ "^/" partition "$" { print $1 }' /etc/fstab
  17.        
  18. partitions=( /usr /home /var /tmp )
  19. awk '
  20.     FNR==NR { partitions[$0]=""; next }
  21.     $1 !~ /^#/ && ($2 in partitions) { print $1 }
  22. ' <(printf '%sn' "${partitions[@]}") /etc/fstab
  23.        
  24. declare -A 'partitions=([/usr]= [/home]= [/var]= [/tmp]=)'
  25. while read -r spec file vfstype mntops freq passno; do
  26.     [[ $spec != #* && ${partitions[$file]+set} ]] && echo "$spec"
  27. done < /etc/fstab
  28.        
  29. dirs=( /usr /home /var /tmp )
  30. for dir in "${dirs[@]}"; do
  31.     { read -r; read -r part _; } < <(df -P "$dir")
  32.     echo "$part"
  33. done
  34.        
  35. awk '$2 ~ "'${PARTITIONS[$n]}'" { print $1 }' /etc/fstab
  36.        
  37. awk "$2 ~ /${PARTITIONS[$n]////\/}/ { print $1 }" /etc/fstab
  38.        
  39. PARTITIONS=(/usr /home /var /tmp)
  40. awk -v partition="${PARTITIONS[*]}"
  41.   '$2 != "" && partition ~ $2"\>" { print $1 }' /etc/fstab
  42.        
  43. $ cat file | program # would normally just be ...
  44. $ program < file
  45.        
  46. (echo StartFlag ${PARTITIONS[*]}; cat /etc/fstab) | awk ...
  47.        
  48. awk -v partitions="${PARTITIONS[*]}" '
  49.     BEGIN { split(partitions,a," ") }
  50.     { for (e in a) { if ($2 ~ a[e]) { print $1 } } }' /etc/fstab