awk to get values greater than


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to get values greater than
# 1  
Old 10-11-2013
awk to get values greater than

data.txt
Code:
August 09 17:16 2013
August 09 17:17 2013
August 09 17:19 2013
August 09 17:20 2013
August 09 17:21 2013
August 09 17:22 2013
August 09 17:23 2013
August 09 17:24 2013

to print from a point in this file, to the end of the file, i type:

Code:
awk '/August 09 17:22/,0' data.txt.

the value "August 09 17:22" is a value supplied by the user to the script, from the command line. So since the user supplied that value, and that value actually existed in the data.txt file, when i typed the awk command, i got an output back.

now, what happens if the user supplied a value that doesn't exist?

Code:
awk '/August 09 17:18 2013/,0' data.txt

You'll get no response.

what i've been stuck on is, when the date supplied by the user does not exist in the data file, i want awk to grab the date that is the closest to the date the user supplied.

In this case, since "August 09 17:18 2013" was not found, the date that should have been grabbed instead should be "August 09 17:17 2013". And awk should print from that line to the end of the file.
# 2  
Old 10-11-2013
Code:
#!/bin/bash
#
#

# check command-line for correct usage
if [ $# -ne 1 ]
then
    echo "Usage: ${0##*/} <filename>"
    exit 1
fi

# store the filename
dataFile=$1

# prompt for search date string
read -p "Enter Search Date [i.e. August 09 17:15 2013]: " dateString

# confirm something was entered
if [ ! "$dateString" ]
then
    echo "Must Enter A Value."
    exit 1
fi

# lets grep to see if the string is in the file
statString=$(grep -c "$dateString" "$dataFile")

# if it's there, then that was easy, if not,
# then let's begin substracting 1 minute from the time
# until we find a match
if [ $statString -eq 1 ]
then
    awk "/$dateString/,0" "$dataFile"
else
    while :
    do
        newdateString=$(date '+%B %d %H:%M %Y' --date "$dateString EDT -1 min")
        unset statString
        statString=$(grep -c "$newdateString" "$dataFile")
        if [ $statString -eq 1 ]
        then
            awk "/$newdateString/,0" "$dataFile"
            exit 0
        fi
    done
fi

# done
exit 0

./srchDate.sh data.txt
Enter Search Date [i.e. August 09 17:15 2013]: August 09 17:22 2013
August 09 17:22 2013
August 09 17:23 2013
August 09 17:24 2013

./srchDate.sh data.txt
Enter Search Date [i.e. August 09 17:15 2013]: August 09 17:18 2013
August 09 17:17 2013
August 09 17:19 2013
August 09 17:20 2013
August 09 17:21 2013
August 09 17:22 2013
August 09 17:23 2013
August 09 17:24 2013

These 2 Users Gave Thanks to in2nix4life For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace the field values, which are greater than the specified value with TRUE?

I have a csv file as given below, org1 org2 org3 org4 org5 gene1 100 80 90 80 150 gene2 30 70 50 50 115 gene3 40 120 60 40 105 gene4 20 72 40 60 20 I need to replace the fields are having values greater than 100 with "TRUE". I used the following commands to replace... (6 Replies)
Discussion started by: dineshkumarsrk
6 Replies

2. UNIX for Beginners Questions & Answers

Awk: greater than sign is working upside down

Hi, I noticed a weird behaviour with awk. input: A|B|1-100|blabla_35_40_blabla;blabla_53_60_blabla;blabla_90_110_blabla Objective: For each string separated by ';' in $4, if the first and second numbers are included in the interval in $3, then print "TRUE". Otherwise print "FALSE". In... (3 Replies)
Discussion started by: beca123456
3 Replies

3. Shell Programming and Scripting

How to print values that are greater than 0.1 in at least 80% of the samples?

input sample1 sample2 sample3 sample4 sample5 sample6 sample7 sample8 sample9 sample10 v1 0.2 0.1 0.1 0 1 2 3 4 9 10 v2 0 0 0.01 0 0 0 0 0 0 0 v3 0 0 0 0 0 ... (35 Replies)
Discussion started by: quincyjones
35 Replies

4. Shell Programming and Scripting

Help me to find a greater than or smaller than values

Hi, i need to find one of the value from my file is in between two numbers, that is the value is greater than 34 and smaller than 50, Ex: File.txt col1 col2 col3 col4 1 Name1 93 w 2 Name2 94 a 3 Name3 32 b 4 Name4 45 x 5 Name5 50 y 6 Name6 49 z here i need to find col3 values are... (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

5. Shell Programming and Scripting

AWK: Cannot read Number of records greater than 1(NR>1)

Hi all, I have a tab-delimited text file of size 10Mb. I am trying to count the number of lines using, grep -c . sample.txtor wc -l < sample.txt or awk 'END {print NR}' sample.txtAll these commands shows the count as 1, which means they are reading only the first header line of the file.... (3 Replies)
Discussion started by: mehar
3 Replies

6. Shell Programming and Scripting

Greping the column with a value greater than 50 with awk

Hi All, I am running a command from a remote server using ssh to different servers. I will get a output like below with 4 columns. I want to grab line which is having a coulmn which grate than or equal to 50. How can I do it with Awk or sed ??. I add a space to first row with sed 's/::/:: /g' to... (4 Replies)
Discussion started by: raghin
4 Replies

7. Shell Programming and Scripting

Need to Grep or awk a logfile for greater than value

Hello all Hoping someone would be kind enough to suggest a solution to a problem i have, and see if maybe i can even do this without a script. Essentially i have a very large log file, and within it each line had a value called TTMT, and it records a variable number in the following way, so... (6 Replies)
Discussion started by: 1905
6 Replies

8. Shell Programming and Scripting

Finding absolute values greater than a certain value

Hi I am posting here for the first time. I am trying to write a script that reads a data file and tries to determine if any absolute values that are above 0.5 I was thinking it ought to be possible to do this with awk somehow. Are there any suggestions before I start reinventing the wheel? ... (4 Replies)
Discussion started by: jackygrahamez
4 Replies

9. Shell Programming and Scripting

AWK greater than?

Sorry for such a basic question, but I have spent hours trying to work this out! I need an awk command (or similar) that will look at a text file and output to the screen if the 4th column of each line has a value greater than or equal to x. data.txt This is the 1 line This is the 2 line This... (6 Replies)
Discussion started by: dlam
6 Replies

10. Shell Programming and Scripting

AWK greater than 200

I need to grep out usernames with UID's greater than 200 out of the /etc/passwd file for a server migration. It is the third field however I am not sure to to accomplish this. I tried to search the forums but did not seem to find an answer (might have over looked it). Any help would be... (2 Replies)
Discussion started by: insania
2 Replies
Login or Register to Ask a Question