grep string and then find another string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep string and then find another string
# 1  
Old 02-10-2009
grep string and then find another string

I have a file that looks like this:

Name=TOM
abcded
asdfas
fkoiaerj
inadhah
Name=Chris
23nafds
vasdkfna
afadns
afdadsfa
aaaaaa
bbbbbb
cccccc

I would to search for the string 'bbbbbb', then I would like to search above that string for the 1st occurrence of "Name" and not the other occurrences. In this case I would like the return string to be:

Name=Chris

Thanks in advance!
# 2  
Old 02-10-2009
Tools One approach

Yes, I believe there will be other (better ?) ways with awk. But, the following is an approach that seems to give you the desired output.

Code:
> cat file165
Name=TOM
abcded
asdfas
fkoiaerj
inadhah
Name=Chris
23nafds
vasdkfna
afadns
afdadsfa
aaaaaa
bbbbbb
cccccc

> sed "s/Name/~Name/" file165 | tr "\n" "|" | tr "~" "\n" | grep "bbbbbb" | tr "|" "\n" | grep "Name="
Name=Chris

# 3  
Old 02-10-2009
Code:
nawk -v str='bbbbbb' '/^Name=/ {name=$0;next} $0 == str { print name}' myFile.txt

# 4  
Old 02-10-2009
Thanks for the quick response! The 'sed' command worked perfectly.
# 5  
Old 02-10-2009
One more question in response to the 'sed' command, is there any way to reference a variable name as the search string and as the file name?

Example:

STRING=bbbbbb
FILE=file165

sed "s/Name/~Name/" $FILE | tr "\n" "|" | tr "~" "\n" | grep "$STRING" | tr "|" "\n" | grep "Name="
# 6  
Old 02-10-2009
Hammer & Screwdriver

I think what you showed is ok. Have you tried to run with it?

My only concern would be in your variable names - I like to stay away from defining and using variable names like STRING or FILE and use instead SrchString or SrchFile. That way, you don't have to worry about over-writing some system or other variable that might be using that common word.
# 7  
Old 02-10-2009
Yes, I tried with the variables, but no luck. I did not use the variable names that I put in the example, I just used that naming convention for description.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep to find an exact string

Hi all, I tried searching the forum for this, and I read numerous suggestions here and even on other forums, and I cannot get this to want the way that I need it to. I tried grep -W / -f to no luck. Here is what I have. I have a list of file names- FILE1-FILE1TEST,FILE1RELATION... (7 Replies)
Discussion started by: jeffs42885
7 Replies

2. Shell Programming and Scripting

Sleep till grep find the string

Hello! I have a sample code in which a grep is being performed from multiple files with a date format in their naming convention. Here the script: #! /usr/bin/bash cd /IN/service_packages/SMS/cdr/received MYDATE=`date +"%Y%m%d%H%M"` #get the value then divide by 60 #DAPS_SLC01... (3 Replies)
Discussion started by: nms
3 Replies

3. Shell Programming and Scripting

Grep command to find string in Variable in file2

Hi , I am executing 2 queries and output is saved in file1.txt and file2.txt example of file1.txt Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,, Testing Excel _.xlsx,/Lab/Development and... (3 Replies)
Discussion started by: Sunil Mathapati
3 Replies

4. Shell Programming and Scripting

Recursive find / grep within a file / count of a string

Hi All, This is the first time I have posted to this forum so please bear with me. Thanks also advance for any help or guidance. For a project I need to do the following. 1. There are multiple files in multiple locations so I need to find them and the location. So I had planned to use... (9 Replies)
Discussion started by: Charlie6742
9 Replies

5. UNIX for Dummies Questions & Answers

Using grep to find files that don't contain a string

Hi all, I am still learning my way around unix commands and I have the following question. I have a website and I want to search for all the html pages that don't contain a certain js file. The file I am searching for is located under /topfolder/js/rules.js . So I assume in my grep search I... (5 Replies)
Discussion started by: SyphaX
5 Replies

6. Shell Programming and Scripting

Find a string using grep & print the line above or below that.

Hi All, Please tell me how can I Find a string using grep & print the line above or below that in solaris? Please share as I am unable to use grep -A or grep -B as it is not working on Solaris. (10 Replies)
Discussion started by: Zaib
10 Replies

7. Shell Programming and Scripting

find out line number of matching string using grep

Hi all, I want to display line number for matching string in a file. can anyone please help me. I used grep -n "ABC" file so it displays 6 ABC. But i only want to have line number,i don't want that it should prefix matching context with line number. Actually my original... (10 Replies)
Discussion started by: sarbjit
10 Replies

8. Shell Programming and Scripting

find exarct string using grep

Hi i have t1 file like: HiPerformance: Recalculation Dates|234| HiPerformance: Recalculation|456| ..... ..... .... ... i wrote grep command like: grep "HiPerformance: Recalculation" t1 out put: HiPerformance: Recalculation Dates|234| HiPerformance: Recalculation|456| But i... (3 Replies)
Discussion started by: koti_rama
3 Replies

9. Shell Programming and Scripting

grep string and find line before

hi, i have to grep for string in file but i want to find the group of this line so i must get lines before and select the group. the file look like : ####name_groupe1 alphanumeric line alphanumeric line .. ####name_groupe2 alphanumeric line alphanumeric line .. ####name_groupe3... (4 Replies)
Discussion started by: kamel.seg
4 Replies

10. Shell Programming and Scripting

How to replace all string instances found by find+grep

Hello all Im performing find + grep operation that looks like this : find . -name "*.dsp" | xargs grep -on Project.lib | grep -v ':0' and I like to add to this one liner the possibility to replace the string " Project.lib" that found ( more then once in file ) with "Example.lib" how can I do... (0 Replies)
Discussion started by: umen
0 Replies
Login or Register to Ask a Question