How to search a number in a certain field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search a number in a certain field
# 1  
Old 01-29-2009
How to search a number in a certain field

Hello,

I have a file that looks like this:

Code:
 
num11 num12 num13 word1
num21 num22 num23 word2
num31 num32 num33 word3
.
.
.

I would like to search for the lines that contain a given number, but I need to narrow the search only to the first field, that means that the number has to be here:
Code:
 
num11
num21
num31
.
.
.

can I do it with grep? Or is there another possibility?

Thank you,
Shira.
# 2  
Old 01-29-2009
Code:
nawk '{print $1}' <file> | grep -n <num>

# 3  
Old 01-29-2009
Hi, thanks!
Is there a version withour nawk?
# 4  
Old 01-29-2009
Use awk
# 5  
Old 01-29-2009
For first field:
Code:
grep '^num1' infile

For third field:
Code:
grep '^[^ ]* [^ ]* num3' infile

Or better, use awk:
Code:
awk '{ if($3 == num3 ) print }' infile

Quote:
Originally Posted by shira
Hello,

can I do it with grep? Or is there another possibility?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

Changed Field Number

HI, I have two files and contains many Fields or columns with | (pipe) delimitor, wanted to compare both the files and get only unmatched perticular fields number. ex: first.txt 1 | 2 | 3 111 |abc| 230 hbc | bb2 | cs second.txt 1 | 2 | 3 111 |abc |230 abn | bb2 | fp Here the... (7 Replies)
Discussion started by: prawinmca
7 Replies

3. Shell Programming and Scripting

Get the changed field number

HI, I have two files and contains many Fields or columns with | (pipe) delimitor, wanted to compare both the files and get only unmatched perticular fields number. ex: first.txt 111 |abc| 230| hbc | bb2 | cs second.txt 111 |abc |230 |abn | bb2 | fp Here the different data in two... (2 Replies)
Discussion started by: prawinmca
2 Replies

4. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

5. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

6. Shell Programming and Scripting

Adding total of first field for each number in the second field

Dears, I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field . 17215630 , 0 907043 ,1 201050 ,10 394149 ,4 1964 ,9 17215630, 0 907043 ,1 201050, 10 394149 ,4 1964 ,9 1234234, 55 23 ,100 33 ,67 ... (2 Replies)
Discussion started by: shary
2 Replies

7. Shell Programming and Scripting

find the field number

######################## SOLVED ################## Hi I have a header file like the following and the field "IDENTIFIER" can be at any possition on this line, The line can containt a variable number of field, not alway the same depending of the header file i use ... (6 Replies)
Discussion started by: kykyboss023
6 Replies

8. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

9. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

10. Homework & Coursework Questions

Search last field

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Give 'awk' script which printss last field of every line..everyline may have different number of fields. 2.... (1 Reply)
Discussion started by: aadi_uni
1 Replies
Login or Register to Ask a Question