A little grep help please

A

ally100

Guest
Hi all,

I have a text file containing hundreds of lines and I am trying to retrieve and print just the account names held in this text file.

Using grep -n '^user' filename.txt I can see that one of the accounts called 'user' is first word on one of three lines. The one I'm interested in is line 124. Now i want to be able to just print the account name user and nothing else.

Using this command - grep '^user' filename.txt , I get the following output;

user pts/1 adsl-194-162-fix Thu Jul 9 08:40:04

(note there are about 2 or 3 tabbed spaces between user and pts/1)


So my question is;
A) How do i limit the output to just line 124?
B) How do i print just the first word 'user' and not the remainder of the line?
C) I also need to replicate this for 2 other accounts, can this all be accomplished in one line?

Many thanks in advance
Ally.
 


So you're basically about half way there - you've figured out how to print out line 124 by grepping for ^user.. to print out just the first column of the text, you can use awk..

Code:
grep '^user' filename.txt | awk '{print $1}'
 
So you're basically about half way there - you've figured out how to print out line 124 by grepping for ^user.. to print out just the first column of the text, you can use awk..

Code:
grep '^user' filename.txt | awk '{print $1}'
Perfect, problem solved, many thanks for your help its greatly appreciated.
 
So you're basically about half way there - you've figured out how to print out line 124 by grepping for ^user.. to print out just the first column of the text, you can use awk..

Code:
grep '^user' filename.txt | awk '{print $1}'
I know this problem is already solved, but I'd like to point out that the same thing can be accomplished by passing output from the grep command to the cut command:
Code:
grep '^user' filename.txt | cut -d" " -f1
or if file contains tabs instead of spaces:
Code:
grep '^user' filename.txt | cut -f1
 

Members online


Top