Basic Grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic Grep
# 1  
Old 04-29-2010
Basic Grep

I am trying to grep a section of .txt file...but once I grep the certain area of the file I would like to display all lines below it as well....how do I have to go about doing this...

example

grep "Sidney Crosby" hockey.txt

result
Sidney Crosby

Age
Goals
Assist


Can this be done with grep?
# 2  
Old 04-29-2010
Code:
$ awk '/Sidney Crosby/,0' hockey.txt

Afaik grep will work, but you need to know beforehand how many lines you want to be displayed when the string is found e.g.
Code:
$ grep -A10 "Sidney Crosby" hockey.txt

# 3  
Old 04-29-2010
Perfect! Thank you very much

is there an easy way to make it display only the information up to the next players name?
# 4  
Old 04-29-2010
You're welcome.
There is definitely a way to extract that data (although I'm not sure if I myself can provide you that command, but I will try).
Can you post some sample data with at least 2 or 3 players?
# 5  
Old 04-29-2010
I have not actaully created the text file yet but it will look something like this:

I can add an @ sign before each name if that helps at all

@Sidney Crosby

Goals - 51
Assist - 47
Pts - 98
Country: Canada


@Alex Ovechkin

Goals - 48
Assist - 30
Pts - 78
Country: Russia


@Jordan Stall

Goals - 22
Assist - 39
Pts - 61
Country: Canada

---------- Post updated at 09:08 PM ---------- Previous update was at 09:01 PM ----------

some players will have more lines than others because some will have playoff stats, olympic stats, ect and others wont
# 6  
Old 04-29-2010
In this case it's not necessary to add "@", because the structure is pretty clear and does not vary.
Code:
 grep -A5 "Alex Ovechkin" hockey.txt

Edit: Okay, I've read your update right now regarding varying number of lines....
# 7  
Old 04-29-2010
can i use an UNTIL loop somehow

something like :

until [ line = '^@' ] display lines

i dont know the syntax obv but is that an idea that might work?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic grep question

Shell : Bash shell (Fedora 26) In the below text file (output.txt), I need to find all lines starting with the pattern pc. As you can see, only one line matches this condition (pc hello world). But, my below 3 attempts return wrong output. How do I use the grep command correctly here ? ... (4 Replies)
Discussion started by: kraljic
4 Replies

2. Shell Programming and Scripting

Basic grep -c Question

Hello, I have a csv list which contains a reference and tariff code for every customer we have (~7.2 million) looking something like this: customernumber,tariff 012345678910,T0001 012345678911,T0002 012345678912,T0001-A0001 I have a 2nd list which contains every unique tariff and... (8 Replies)
Discussion started by: Cludgie
8 Replies

3. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

4. Shell Programming and Scripting

Basic use of who, cut, grep and pipes??

Hi, I'm new to shell scripting and i'm trying to write code which contains exactly three pipe commands (e.g. A | B | C) to return a list of the users currently logged into the system. I've been told three useful commands i could use are who, grep and cut. I know 'who' return a list of the users... (4 Replies)
Discussion started by: cylus99
4 Replies

5. Shell Programming and Scripting

basic grep question

I think I am doing this correctly, but it is responding very quickly with no results so I am not sure. I need to do a case insensitive grep of all files in my current directory grep -i <keyword> /my/directory is that correct? (1 Reply)
Discussion started by: guessingo
1 Replies

6. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

7. What is on Your Mind?

Basic...

hi, I am pretty new both to unix and this forum, can anyone help me to give shortcuts to my commands... eg:- instead of "cd /usr/bin" i want to to give " bin " and get to that path. I'm using HP-UX 11.0 abey (2 Replies)
Discussion started by: abey
2 Replies

8. UNIX for Dummies Questions & Answers

Basic

hi, I am pretty new both to unix and this forum, can anyone help me to give shortcuts to my commands... eg:- instead of "cd /usr/bin" i want to to give " bin " and get to that path. I'm using HP-UX 11.0 abey (2 Replies)
Discussion started by: abey
2 Replies
Login or Register to Ask a Question