![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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
|
|
||||
|
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 ![]() |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|