grep string and find line before | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

grep string and find line before

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-14-2007
kamel.seg kamel.seg is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 25 June 2008, 9:09 AM EDT
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
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
alphanumeric line
alphanumeric line
..


al the file is like that :

i want to grep string if i find it in line i want to gret the name_group

i use :

grep -n "string to search" file | cut -f1 -d: to get the line number but i want to go lines before to find the group and store that in variable.

i use script shell.

thanks
Sponsored Links
    #2  
Old 12-14-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 September 2014, 4:44 PM EDT
Location: NM
Posts: 10,211
Thanks: 278
Thanked 796 Times in 743 Posts
How about awk?

Code:
awk ' {
         /^###/ { line=NR; value=$0}
         if(index($0, "string to search")>0) { print line, value}
         } '  inputfilename

Sponsored Links
    #3  
Old 12-14-2007
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 19 September 2014, 2:11 PM EDT
Location: The Netherlands
Posts: 7,716
Thanks: 123
Thanked 537 Times in 507 Posts
Or:


Code:
awk '$0 ~ str{print b}{b=$0}' str="String to find" file

Regards
    #4  
Old 12-16-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 23 July 2014, 3:17 AM EDT
Location: Beijing China
Posts: 1,294
Thanks: 0
Thanked 24 Times in 24 Posts
awk

Hi,

To be honest, i am not quiet sure about your req. I suppose it is as follow, please try it and find whether any help for you.

INPUT:

Code:
name_group1
a
a2
A
sdlfk
34
name_group2
b
b1
dfjkl
e4r
name_group3
dflk
line
this is a good idea

OUTPUT:

Code:
name_group1/name_group2/name_group3 according to your input string to be searched

CODE:

Code:
echo "Input string"
read str
nawk -v s="$str" 'BEGIN{
n=""
print s
}
{
if ($0 ~ /name/)
{
	n=$0
}
if (index($0,s)!=0)
{
	if (n!="")
		print n
	n==""
}
}' filename

Sponsored Links
    #5  
Old 12-17-2007
kamel.seg kamel.seg is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 25 June 2008, 9:09 AM EDT
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
hi,


summer_cherry you understand well my problem but i want to use awk not nawk because it's not supported in linux only in sun can u please try to help me to find the right solution using awk and storing the group_name in variable that i can use after.


thank u

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep a string from input file and delete next three lines including the line contains string in xml greet_sed Shell Programming and Scripting 4 09-28-2011 05:18 AM
Grep a string and write a value to next line of found string angel12345 Shell Programming and Scripting 6 08-16-2011 11:07 AM
grep on string and printing line after until another string has been found rxc23816 Shell Programming and Scripting 7 07-23-2011 05:49 PM
Find a string using grep & print the line above or below that. Zaib Shell Programming and Scripting 10 11-05-2010 08:51 AM
find out line number of matching string using grep sarbjit Shell Programming and Scripting 10 09-09-2009 04:34 AM



All times are GMT -4. The time now is 04:20 AM.