The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep string and next line karthikn7974 Shell Programming and Scripting 7 05-23-2008 06:06 AM
find exarct string using grep koti_rama Shell Programming and Scripting 3 04-22-2008 10:53 AM
How to replace all string instances found by find+grep umen Shell Programming and Scripting 0 12-06-2007 03:52 AM
grep the string with the line number salaathi Shell Programming and Scripting 4 12-04-2007 11:42 AM
grep a string in a line using sed viadisky UNIX for Dummies Questions & Answers 4 02-06-2007 06:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-14-2007
kamel.seg kamel.seg is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 57
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
  #2 (permalink)  
Old 12-14-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,813
How about awk?

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

  #3 (permalink)  
Old 12-14-2007
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Or:


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

Regards
  #4 (permalink)  
Old 12-17-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,099
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

  #5 (permalink)  
Old 12-17-2007
kamel.seg kamel.seg is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 57
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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0