Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

grep string and find line before

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 12-14-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 26 September 2016, 9:27 AM EDT
Location: NM
Posts: 10,823
Thanks: 446
Thanked 967 Times in 898 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 Unix and Linux 12-14-2007
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 23 September 2016, 8:57 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
Or:


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

Regards
    #4  
Old Unix and Linux 12-16-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 20 September 2016, 2:38 AM EDT
Location: Beijing China
Posts: 1,305
Thanks: 0
Thanked 26 Times in 26 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 Unix and Linux 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

Linux
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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:45 PM.