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
How about awk?
awk ' {
/^###/ { line=NR; value=$0}
if(index($0, "string to search")>0) { print line, value}
} ' inputfilename
Or:
awk '$0 ~ str{print b}{b=$0}' str="String to find" file
Regards
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:
name_group1
a
a2
A
sdlfk
34
name_group2
b
b1
dfjkl
e4r
name_group3
dflk
line
this is a good idea
OUTPUT:
name_group1/name_group2/name_group3 according to your input string to be searched
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