grep for a search string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep for a search string
# 8  
Old 03-12-2008
Quote:
Originally Posted by raga
HI,
awk '/pattern/{c=11}c&&c--' file
this actually returned me all the 11 lines from the occurance of the pattern in the file.
But i didnt understand how it is working?can you please explain me.

Thank you.
First adjust it to work properly,
you mean 10 lines including the pattern?

Code:
awk '/pattern/{c=10}c&&c--' file

# 9  
Old 03-12-2008
yes.i adjusted for 10.but not understanding the command
{c=10}c &&c--
can you please explain me the above part,so that i can fine tune for any of my requirement.

Thanks for your help
# 10  
Old 03-12-2008
Set c to 10 when the current record matches the pattern,
then decrease c and print the records while c is greater than 0.
# 11  
Old 03-12-2008
Java

Quote:
Originally Posted by radoulov
Code:
awk '/pattern/{c=11}c&&c--' file

Great!Can you please explain how this command is working?
# 12  
Old 03-12-2008
Quote:
Originally Posted by DILEEP410
Great!Can you please explain how this command is working?
See my reply above.

May be this one from Ed Morton should be easier to understand:

Quote:
Set a counter to the number of lines you want to print
and decrement it once per line to zero, invoking the default
action of printing the current line as long as it's non-zero.
An example to make it clear:

Code:
$ cat file
line1
line2
line3
pattern
line1 OK
line2 OK
line3 OK
line4
line5
$ nawk 'c{print c--,$0}/pattern/{c=3}' file
3 line1 OK
2 line2 OK
1 line3 OK

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Search string or words in logs without using Grep

I'm in need of some kind of script that will search for a string in each logfile in a directory but we don't want to use GREP. GREP seems to use up to much of our memory causing the server to use up a lot of swap space. Our log files are bigger than 500M on a daily basis. We lately started... (8 Replies)
Discussion started by: senormarquez
8 Replies

3. Shell Programming and Scripting

Grep string search

Hi All, I'm using aix flavour unix where im trying for the below kind of pattern to search in all the files in a directory. I tried with different possible combinations like grep -ir "out.*\transaction_ctry_code" * etc.... Pattern I'm trying for : out<any thing>country_cd Here i... (0 Replies)
Discussion started by: rmkganesh
0 Replies

4. Shell Programming and Scripting

Search text file, then grep next instance of string

I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line1 Line2 ...... (4 Replies)
Discussion started by: Akilleez
4 Replies

5. UNIX for Dummies Questions & Answers

How to search for string A OR string B using Grep?

Hi, This is what I have done so far which is wrong for some reason: grep -h "stringA | stringB" filename Thank in advance! (5 Replies)
Discussion started by: jboy
5 Replies

6. Shell Programming and Scripting

String: Grep / SED for multy line search

Hi, At first I want to please you to provide the solution with grep/sed if possible. :cool: File looks like: wished result: so I want in a new file BLUE@@RED string from first line like: grep "/folder_start" cs_src > tmp1 string from second line: grep "/main" cs_src... (14 Replies)
Discussion started by: unknown7
14 Replies

7. AIX

grep not working when search string has a space in it

Hi, I am trying to grep a string which has two words separated by space. I used a script to grep the string by reading the string in to a variable command i used in the script is echo "enter your string" read str grep $str <file> it is working fine when the entered string is a single... (3 Replies)
Discussion started by: sekhar gajjala
3 Replies

8. Shell Programming and Scripting

How to search (grep?) filename for a string and if it contains this then...

Hi i want to write a script that will search a filename e.g. test06abc.txt for a string and if it contains this string then set a variable equal to something: something like: var1=0 search <filename> for 06 if it contains 06 then var1=1 else var1=0 end if but in unix script :) (4 Replies)
Discussion started by: tuathan
4 Replies

9. UNIX for Dummies Questions & Answers

grep exact string/ avoid substring search

Hi All, I have 2 programs running by the following names: a_testloop.sh testloop.sh I read these programs names from a file and store each of them into a variable called $program. On the completion of the above programs i should send an email. When i use grep with ps to see if any of... (3 Replies)
Discussion started by: albertashish
3 Replies

10. Shell Programming and Scripting

grep: RE error 41: No remembered search string.

Hi guys, I am currently facing a problem with my current script, the script basically monitor a list of process on the Unix system. If a process is down it will send a notification e-mail to me. Currently I am facing an error when running the script grep: RE error 41: No remembered search... (2 Replies)
Discussion started by: fkaba81
2 Replies
Login or Register to Ask a Question