How to search for numbers greater than x?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search for numbers greater than x?
# 1  
Old 01-02-2013
How to search for numbers greater than x?

I have a file with multiple fields, example below

Code:
File 1:

Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|100
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|101
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|102
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|103
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|104
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|105
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|106
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|107
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|108
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|109
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|110
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|111
Field 1|Field 2|Field 3|Field 4|Field 5|Field 6|Field 7|112

I want to be able to do a search through each line and get only the first field of any line that has the last field with a number greater than or equal to "x". For example lets say x=105, I want the output of this script to be

Code:
Output:
Field 1|105
Field 1|106
Field 1|107
Field 1|108
Field 1|109
Field 1|110
Field 1|111
Field 1|112

# 2  
Old 01-02-2013
Code:
awk -F'|' -v X=105 '$NF>=X{print $1,$NF}' OFS='|' filename

Or simply
Code:
awk -F'|' '$NF>=105{print $1,$NF}' OFS='|' filename

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-02-2013
Thanks, now what if I want to change the number from 105? Is there a way to make that number a variable instead?
# 4  
Old 01-02-2013
try using bipinajith's first example:
Code:
var=105
awk -F'|' -v X=$var '$NF>=X{print $1,$NF}' OFS='|' filename

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 01-02-2013
ahh, I see. Thank you both
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Search for columns with numbers greater than 70

This helped get me started. https://www.unix.com/unix-for-dummies-questions-and-answers/157026-need-command-search-numbers-greater-than-3000-a.html This is the command I am using. I am trying to find numbers greater than 70 in column 5. I do not know if it is getting confused because there... (6 Replies)
Discussion started by: cokedude
6 Replies

2. UNIX for Dummies Questions & Answers

Grep lines with numbers greater than 2 digits at the end of the line

I'm trying to grep lines where the digits at the end of each line are greater than digits. Tried this but it will only allow me to specify 2 digits. Any ideas would greatly be appreciated. grep -i '\<\{3,4,5\}\>' file ---------- Post updated at 05:58 PM ---------- Previous update was at 05:41... (1 Reply)
Discussion started by: jimmyf
1 Replies

3. Shell Programming and Scripting

Search within file1 numbers from list in file2

Hello to all, I hope somebody could help me with this: I have this File1 (real has 5 million of lines): Number Category --------------- -------------------------------------- 8734060355 3 8734060356 ... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

4. Shell Programming and Scripting

search file for value greater than 'n' on each line and email

Hi, I need to write a script that will read each line of a CSV file, look for values greater than x seconds and email an alert. For the first part, I have one CSV per day, each line in the CSV has comma separated values. There are a total of 8 fields per line separated by commas. 6th and 7th... (3 Replies)
Discussion started by: ssid
3 Replies

5. Shell Programming and Scripting

search column and delete row if greater than value

Hi, as the title states i need to find a way to search a column for values great than 1000, and if it is, then delete that row. An example 1 7.021 6.967 116.019 4 U 6.980E+07 0.000E+00 e 0 0 0 0 2 8.292 7.908 118.063 3 U 1.440E+07 0.000E+00 e 0 821 814 ... (3 Replies)
Discussion started by: olifu02
3 Replies

6. Shell Programming and Scripting

How search a list of numbers from a file

Hi Guys! I have two input files. I want to search each of the numbers (studentid) on inputfile1 from inputfile2 and print the studentid and section that are listed from inputfile1. See the desired output below. inputfile1.txt: studentid 261054689 266605695 269826642 264966513... (2 Replies)
Discussion started by: pinpe
2 Replies

7. Shell Programming and Scripting

Search only numbers in a file

Hello team, Suppose I have a file named that has following content: abc123 1897 msxi12 ------------ ######## ((((( rapjagc ))))))) 34229 xxxxxxxxxxxx @@@536 I need those lines that contain only numbers from this file. means -- it should eliminate alphanumber, special characters... (3 Replies)
Discussion started by: singhabm
3 Replies

8. UNIX for Dummies Questions & Answers

need a command to search for numbers greater than 3000

i have a file contains: 13213,A,300 3423,C,200 5563,A,201 3000,A,400 3000,A,402 3000,A,206 3000,A,303 3000,A,200 4233,N,204 i need to search for numbers in the first column are greater than 3000? i have another issue if you can help me? if i want to search in the second or the... (7 Replies)
Discussion started by: takyeldin
7 Replies

9. Shell Programming and Scripting

replacing numbers greater than 0 with 1

I have some ASCII files containing numerous numbers. What I'd like to do is replace all numbers greater than 0 with 1. Examples of the numbers include: - 000011 and 000042 Thanks (4 Replies)
Discussion started by: vrms
4 Replies

10. Shell Programming and Scripting

How to perform calculations using numbers greater than 2150000000.

Could someone tell me how to perform calculations using numbers greater than 2150000000 in Korn Shell? When I tried to do it it gave me the wrong answer. e.g. I have a ksh file with the contents below: --------------------------------- #!/bin/ksh SUM=`expr 2150000000 + 2` PRODUCT=`expr... (3 Replies)
Discussion started by: stevefox
3 Replies
Login or Register to Ask a Question