Grep based on first field


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep based on first field
# 1  
Old 05-01-2014
Lightbulb Grep based on first field

I want to grep based on first column and print all the columns. for eg.

Code:
root       12344       /sh
root       33389       /bash
oracle      87378      /tech/oracle
oracle      26367      /tmp/oracle

Now I want to grep based on user "root" in first column and print like below.

Code:
root       12344       /sh
root       33389       /bash

Please help me.

Last edited by Scrutinizer; 05-01-2014 at 02:23 PM.. Reason: CODE tags
# 2  
Old 05-01-2014
Code:
awk -v val='root' '$1 == val' myFile

# 3  
Old 05-01-2014
Code:
cat file| grep root*

file
Code:
root 12344 /sh
root 33389 /bash
oracle 87378 /tech/oracle
oracle 26367 /tmp/oracle

output
Code:
root 12344 /sh
root 33389 /bash

# 4  
Old 05-01-2014
Quote:
Originally Posted by Makarand Dodmis
Code:
cat file| grep root*

You keep doing this, this is a useless use of cat. Almost no commands need cat's help to read an individual file.

Also, your regex is wrong. grep doesn't work that way. Your regex would match "root", "roott", "roottttttttt", and so forth. Which doesn't actually change anything since you don't have the regex anchored anywhere. You might as well have left the * out completely...

Code:
grep "^root" file

Please consider a little more carefully before giving solutions that are worse than ones already given.
# 5  
Old 05-01-2014
the value 'root' can be in any field. I want to grep the value root only in field-1 and print all the fields based on that.
# 6  
Old 05-01-2014
The expression I gave, "^root", anchors "root" to the beginning of the line.

The awk solution tests against the first token.
# 7  
Old 05-01-2014
Slight modification:
Code:
grep -w "^root" file

OR
Code:
grep "^root " file

This User Gave Thanks to Yoda 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

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. Shell Programming and Scripting

Replacing field based on the value of other field

Hi Team, In the below input file, if I have the value 23,24,25 then for those records 1st field value should get updated from "a" to "b". I also want to pass these values in file as input as it can be done dynamically. Tried awk commands but not getting desired output.Using SunOS 5.10 version.... (14 Replies)
Discussion started by: weknowd
14 Replies

3. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

awk to update value in field based on another field

In the tab-delimeted input file below I am trying to use awk to update the value in $2 if TYPE=ins in bold, by adding the value of HRUN= in italics. In the below since in line 1 TYPE=ins the 117282541 value in $2 has 6 added because that is the value of HRUN=. Hopefully the awk is a start but I... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

Sorting based on the second field

Oracle Enterprise Linux 6 This is my file. Two fields separated by space $ cat testfile.txt MARCH9 MARCH4 MARCH1 MARCH5 MARCH2 MARCH326 MARCH821 MARCH7 MARCH6 MARCH2 $ $ The following numeric sort, based on the first field's 6th character works as expected. $ $ sort -n -k 1.6... (7 Replies)
Discussion started by: John K
7 Replies

6. Shell Programming and Scripting

sorting based on a field

the below is sorted as it is. the fields that i'm interested in are the 4th and 5th field. i want to sort the based on the 4th field. my past attempt to do this was to do something like this: awk '{print $4}'| awk '{print $1":"$2}' datafile | sort | uniq however, if i do that, i lose... (2 Replies)
Discussion started by: SkySmart
2 Replies

7. Shell Programming and Scripting

Uniq based on first field

Hi New to unix. I want to display only the unrepeated lines from a file using first field. Ex: 1234 uname1 status1 1235 uname2 status2 1234 uname3 status3 1236 uname5 status5 I used sort filename | uniq -u output: 1234 uname1 status1 1235 uname2 status2 1234 uname3 status3 1236... (10 Replies)
Discussion started by: venummca
10 Replies

8. Shell Programming and Scripting

Decision based on field value

Hi, I need to change the 3rd field value based on the 4th field value using a shell script. here are the fields; 05:15-23:59; 05:15-21:00; 11:00-18:00; TGSI; TGSummaryInfo_C ; ENTER VALID GAC 05:15-23:59; 05:15-21:00; 11:00-18:00; TGSI; TGSummaryInfo_E ; ENTER VALID GAC 05:15-23:59;... (4 Replies)
Discussion started by: sam_bd
4 Replies

9. UNIX for Dummies Questions & Answers

awk - Summing a field based on another field

So, I need to do some summing. I have an Apache log file with the following as a typical line: 127.0.0.1 - frank "GET /apache_pb.gif HTTP/1.0" 200 2326 Now, what I'd like to do is a per-minute sum. So, I can have awk tell me the individual minutes, preserving the dates(since this is a... (7 Replies)
Discussion started by: treesloth
7 Replies

10. Shell Programming and Scripting

Find top N values for field X based on field Y's value

I want to find the top N entries for a certain field based on the values of another field. For example if N=3, we want the 3 best values for each entry: Entry1 ||| 100 Entry1 ||| 95 Entry1 ||| 30 Entry1 ||| 80 Entry1 ||| 50 Entry2 ||| 40 Entry2 ||| 20 Entry2 ||| 10 Entry2 ||| 50... (1 Reply)
Discussion started by: FrancoisCN
1 Replies
Login or Register to Ask a Question