1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with Unix -- Awk command

  1. Sep 15, 2015 #1

    RJLiberator

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    use awk to create a subset of the original data file called subset.gmt that only contains records with longitudes between -112 and -102. Hint: you can use the operator && to tell awk to look for more than one pattern at once.

    2. Relevant equations


    3. The attempt at a solution

    What we need is to find an awk command that allows me to take the data from a previous file, the column 1 (longitude) and take only the records between -112 and -102.

    So, I understand we start the command as such:

    awk 'NR>1{print $1.....}' originalfile.txt >! newoutput.ps

    The column that I want to pull from is 1 from the file.
    But how do I create a code such that it pulls only the records between -112 and -102?
    Is it an if statement? Professor mentions the use of boolean operator &&, but I've searched the internet with no luck...

    Thank you.
     
  2. jcsd
  3. Sep 15, 2015 #2

    NascentOxygen

    User Avatar

    Staff: Mentor

    Presumably, the values in column 1 will not be all neat integers?

    I think your first move should be to invent your own sample data file containing a representative selection of records, including a couple where the relevant field is, say, -112 or something. Then focus on devising the code for selecting those records where the desired field equals that value, -112

    Once you have this working, you can build on it to select for a range of values, and not necessarily integers. Looking at short sample awk scripts in your class notes or online is the best way to learn how this can be achieved.

    You have an awk utility at home that you can keep testing your script on? Study manuals all you like, but nothing is as valuable to learning as the immediacy of being able to "guess & test".
     
  4. Sep 15, 2015 #3

    RJLiberator

    User Avatar
    Gold Member

    Yes, I have a virtual machine which allows me to use linux to study unix and the awk commands here.

    I can't confirm if they are neat integers or not, they may be, I will be able to confirm that tomorrow. :)

    I will check through some sample class code, but after doing so earlier, I didn't have much luck in finding a way to isolate part of the column results in such a manner.
    What could the && operator be used for?
     
  5. Sep 16, 2015 #4

    NascentOxygen

    User Avatar

    Staff: Mentor

    Google will find plenty of resources, e.g., search on "unix awk tutorial".
     
  6. Sep 16, 2015 #5

    RJLiberator

    User Avatar
    Gold Member

    A friend of mine suggested something along the nature of :

    awk 'NR>1{print $1 -112&&-102}' originalfile.txt >! newoutput.ps

    but not sure how to make it 'search' for the values inbetween -112 and -102

    Perhaps:

    awk -F'[:}]''$(NF-1) >= -112&& $(NF-1) <= -102' file > output.txt
     
  7. Sep 16, 2015 #6

    RJLiberator

    User Avatar
    Gold Member

    Boom.
    Got it.
    Sample code:
    Code (Text):
    awk '$1>=-112&&$1<=-102{print}' EX1.csh > subset11.gmt
    What this does is it takes column 1 and says all values between -112 and -102 you will print.
     
  8. Sep 17, 2015 #7

    NascentOxygen

    User Avatar

    Staff: Mentor

    Looks good. You will find that you can even omit the {print} command because printing the whole record is the default action for AWK.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Help with Unix -- Awk command
  1. Unix script problem (Replies: 1)

Loading...