×
Create a new article
Write your page title here:
We currently have 3,189 articles on s23. Type your article name above or create one of the articles listed here!



    s23
    3,189Articles
    < Awk
    Revision as of 16:41, 26 May 2006 by imported>DrOwl
    (diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

    Search for $var in * files

    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

    1. !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


    [code] listOfValues.txt

    big cup sox [/code]


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


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


    [code] listOfMatchedData.txt big, size, notsmall cup, thing sox, magic, longsox [/code]

    Cookies help us deliver our services. By using our services, you agree to our use of cookies.
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.