Grep based on first field


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

Code:
cat testfile.txt | grep -w '^root'

or
Code:
cat testfile.txt | grep '^root'

i tried both the ways, no output for the above commands.

Last edited by Scrutinizer; 05-01-2014 at 02:22 PM.. Reason: CODE tags
# 9  
Old 05-01-2014
The only reason for not getting any output is that there are no records that starts with root

Check your input file carefully for any blank spaces or other characters.
# 10  
Old 05-01-2014
I get record for the below command.

Code:
cat testfile.txt  | grep 'root'


The moment i add the special character "^", no records showing out.

---------- Post updated at 01:22 PM ---------- Previous update was at 01:01 PM ----------

I tried like below, it worked.

Code:
cat testfile.txt  | awk '$1 == "root"'

but how to use shell variable instead of literal name in the place of "root".

Last edited by Scrutinizer; 05-01-2014 at 02:26 PM.. Reason: CODE tags
# 11  
Old 05-01-2014
Quote:
Originally Posted by baladelaware73
[..]
but how to use shell variable instead of literal name in the place of "root".
See post #2
# 12  
Old 05-01-2014
And please no UUOC!
Other variants:
Code:
<testfile.txt awk '$1 == "'"$var"'"'

Better readable is
Code:
awk '$1==key' key="$var" testfile.txt

# 13  
Old 05-01-2014
Code:
perl -slane 'print if $F[0] eq $pat' < file -- -pat='root'

# 14  
Old 05-01-2014
Quote:
Originally Posted by baladelaware73
I get record for the below command.

Code:
cat testfile.txt  | grep 'root'

The moment i add the special character "^", no records showing out.
The reason is most probably that "root" is the first field, but not the first characters on the line:

Code:
root     blabla .....
 root    blabla .....

The expression will find the first line, but not the second, because of the leading blank. "^" means "begin of line" and "^root" means "begin of line, immediately followed by r-o-o-t". This is the case in the first example, not the second.

If you want to allow for optional blanks at the beginning of line modify the regular expression this way ("<b>" denotes a literal blank, "<t>" a tab character):

Code:
grep '^[<b><t>]*root' /some/file'

This means "begin of line, followed by zero or more space- or tab-characters (=white space), followed by the string r-o-o-t". This would find both lines in the example above.

I hope this helps.

bakunin
 
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