Visit The New, Modern Unix Linux Community


retrieving specific lines from a file - can I use grep ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting retrieving specific lines from a file - can I use grep ?
# 1  
retrieving specific lines from a file - can I use grep ?

Hi there, if i had a file that looked like this

Code:
my_server1
   red
   green
   blue
   yellow
   blue

my_server2 

  blue
  blue
  yellow
  green
  blue

my_server3

  yellow
  yellow
  blue

and I wanted to be able to grep for the hostname line and every instance of , say the word blue that appear below that hostname (but above the next hostname) ...what tool could i use ...it seems to me that grep doesnt seem to have the ability to be as specific as I require ...is there anything else ?

I want to get an output like this

Code:
my_server1
   blue
   blue
my_server2 
  blue
  blue
  blue
my_server3
  blue


any help on this would be great
# 2  
Code:
grep 'my_server\|blue' file

# 3  
Quote:
Originally Posted by danmero
Code:
grep 'my_server\|blue' file

OK, forgot to mention im running on Solaris, the above command returns nothing at all
# 4  
Code:
grep 'blue' my_server* | awk -F: '$1!=p1{print $1;} {print $2;p1=$1;p2=$2}'

# 5  
thanks for the reply, it didnt work so i removed the last bit, the awk statement (see below) and even the grep on its own is not returning anything
Code:
cat file | grep 'blue' my_server*


to be honest im a bit lost with this one
# 6  
I give you 3 options here:
Code:
grep -e my_server -e blue file
awk '$0 ~ "my_server|blue"' file
sed '/myserver\|blue/p;d' file

# 7  
Thankyou very much for taking the effort to give me those three options..I have tried them all with no success though ... I thank you anyway


Code:
# grep -e my_server -e blue /opt/my_file
grep: illegal option -- e
Usage: grep -hblcnsviw pattern file . . .

# awk '$0 ~ "my_server|blue"' /opt/my_file
awk: syntax error near line 1
awk: bailing out near line 1

# sed '/my_server\|blue/p;d' /opt/my_file
#     <<< no output


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #272
Difficulty: Easy
Alan Turing, while working at Bletchley, occasionally ran the 40 miles to London when he was needed for meetings there.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep content between specific lines

cat file1 *FileHeader* Partition 0 Total Data Bytes 1416 Avg Bytes/Record 1416 Others 1 PRDX22.AUDIT_DATA_INFO Partition 4 Total Data Bytes 4615 Avg... (8 Replies)
Discussion started by: Veera_V
8 Replies

2. UNIX for Dummies Questions & Answers

Grep specific lines

Hello I have a file with nearly 90000 lines in x,y,z format but have some lines that I do not need to show. Is there anyway to delete those 3 lines after every 288 lines. Eg I keep the first 288 lines delete (289, 290 291); keep the next 288 lines after those and so on... Thanks (6 Replies)
Discussion started by: Madiouma Ndiaye
6 Replies

3. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

4. Shell Programming and Scripting

Grep all lines for a specific date in log-files

I need to grep all lines for "yesterday" in /var/log/messages. Dates are in the format "YYYY-MM-DD". (5 Replies)
Discussion started by: Padmanabhan
5 Replies

5. Shell Programming and Scripting

grep the output between specific lines

Symmetrix ID : 00000001234 Host Name : myown Identifiers Found : 5000000000000000 5000000000000001 Device Cap(MB) Attr Dir:P ------ ------- ---- ---- 1234 25886 (M) 8D:1, 9D:1 0123 25886 (M) 8D:1, 9D:1 1345 25886 (M) ... (5 Replies)
Discussion started by: maddy.san
5 Replies

6. Shell Programming and Scripting

Grep only specific lines ordered by column/date

Hi everybody, I'd like to think I've been through the search tool not only on this site, but also on google too, but I haven't been able to find what I was looking for. If I might've missed something on this forum, please slap me in the face with a link that you consider useful for my query :D ... (4 Replies)
Discussion started by: dilibau
4 Replies

7. Shell Programming and Scripting

Problems to print specific lines with awk and grep...HELP!

Hi all I have data like this: model: 1, misfit value: 0.74987 1 1.182 1.735 2.056 1.867 2 0.503 1.843 2.018 1.888 3 2.706 2.952 2.979 1.882 4 8.015 3.414 3.675 1.874 ... (1 Reply)
Discussion started by: fedora2011
1 Replies

8. Shell Programming and Scripting

Retrieving a specific value

I have this input file: I want to get the following output file: Output Basically, the program would scan the file and look for every instance where the value is < 14.0 dB. The program would then print the row that contains that value along with the corresponding name with the ">". Be... (8 Replies)
Discussion started by: Ernst
8 Replies

9. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

10. UNIX Desktop Questions & Answers

grep lines with two specific characters somewhere in the line

I'm having trouble with extracting certain lines from a file based on whether they have all the required fields. Original file: snt:594:Sam N This bpt:2342:Bob P That lr:123 wrp:23:Whoever Person cor:794 Desired output: snt:594:Sam N This bpt:2342:Bob P That wrp:23:Whoever Person ... (3 Replies)
Discussion started by: Chthonic
3 Replies

Featured Tech Videos