×
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/examples: Difference between revisions

    < Awk
    Content added Content deleted
    imported>DrOwl
    No edit summary
    imported>DrOwl
    (more examples...)
     
    (One intermediate revision by the same user not shown)
    Line 31: Line 31:




    <pre>
    [code]
    listOfValues.txt
    listOfValues.txt


    Line 37: Line 37:
    cup
    cup
    sox
    sox
    </pre>
    [/code]




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






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




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



    ==List the partitions using more than 70% of disk partition space .==
    $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==
    if field 2 "starts with LR" and "ends with -A"

    #!/bin/ksh

    awk -F, ' {
    if ( $2 ~ /^LR-/ && $2 ~ /-A$/ ) {
    print $2
    }
    } ' output.txt > outputA.txt

    Latest revision as of 09:25, 25 September 2008

    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

    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.