Awk/examples

From s23
< Awk
Jump to navigation Jump to search

Search for $var in * files[edit]

This script get a value from listOfValues.txt and searches through all .dat files in the /data directory it pulls out the data in Colom 4 and apends it to one line of output we should really use Vars instead of filensmes and all that nice stuff but hay =)

searchData.ksh
#!bin/ksh
echo > listOfMatchedData.txt

for x in `cat listOfValues.txt`; do

awk ' $4 ~ /'$x'/ {found=found", "$1};
END { print "'$x'" found } '  data/*.dat >> listOfMatchedData.txt

done

$4 means test colom 4

~ = partal match

/'$x'/ = /y/ says patern match, '$x' i used quotes becouse im using a varable from outside of awk.

{found=found", "$1} apends a , and data from colom 1 to the varible found

END = exacutes this code after the last file has been closed

{ print "'$x'" found } prints out $x (the value we were searching for) again i quoted this value as before and the found data


listOfValues.txt

big
cup
sox


data/2005.dat
size 0 t itsbig
thing 1 e teacup
magic 2 g holysox


data/2006.dat
notsmall 0 t itsbig
FnoRd 1 e teabag
fNORd 2 g longsox


listOfMatchedData.txt
big, size, notsmall
cup, thing
sox, magic, longsox


List the partitions using more than 70% of disk partition space .[edit]

$5 represants column number to be compared and 70 is the value to be compared .

df -k | awk '$5 > 70'

Match a field and do something[edit]

if field 2 "starts with LR" and "ends with -A"

  1. !/bin/ksh

awk -F, ' {

 if ( $2 ~ /^LR-/ && $2 ~ /-A$/ ) {
   print $2
 }

} ' output.txt > outputA.txt