How do i use basic level awk?



  • This explains using awk at a very basic level.

    The way awk works is similar to sed, it will execute your instructions on one line at a time across the entire file.

    awk 'awk commands' files

    or if your awk commands are in a script, you can call it like this.

    awk -f script files

    Example on the command line.

    Still using our sample file called data_file

    hamster, furry little creature, 2 hamster drive
    donkey, back breaking helper, 8 donkey jacket road
    horse,  top racer, 12 Horse throat Lane
    badger,  night rider, 15 Badger Close 
    cow,  love eating grass all day, 16 Moofield Aveune
    sheep, can you take this coat off, 6 Wool road
    

    The awk commands have to be in curly brackets, as there is no pattern match awk applied the awk commands to all lines

    $ awk '{print $1}' data_file 
    hamster,
    donkey,
    horse,
    badger,
    cow,
    sheep,
    

    To take the comma off the end we use the awk "-F" field separator options

    $ awk -F, '{print $1}' data_file
    hamster
    donkey
    horse
    badger
    cow
    sheep
    

Log in to reply
 

© Lightnetics 2024