Need one liner to search pattern and print everything expect 6 lines from where pattern match made


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need one liner to search pattern and print everything expect 6 lines from where pattern match made
# 1  
Old 03-15-2012
Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made.
# 2  
Old 03-15-2012
awk

Hi,

Try this one,

Code:
awk -v pat="YourPatternStr" -v cnt=6 '{a[NR]=$0;}END{for(i=1;i<=NR;i++){print a[i];if(a[i] ~ pat ){i=i+cnt;}}}' file

Cheers,
RangaSmilie
# 3  
Old 03-15-2012
it works.. but..

Hi ,

It works but i am trying something like this.. and it removes the original file. why so ?
Code:
ssh $i "/usr/xpg4/bin/awk -v pat="CMDS=" -v cnt=6 '{a[NR]=$0;}END{for(i=1;i<=NR;i++){print a[i];if(a[i] ~ pat ){i=i+cnt;}}}'  sudofile > sudofilecopy"

when i check it empties sudofile content
# 4  
Old 03-15-2012
Code:
$ cat -n a.txt
     1	one
     2	two
     3	three
     4	four
     5	five
     6	six
     7	seven
     8	eight
     9	nine
    10	ten
    11	eleven
$ awk '/two/{for(i=0;i<=5;i++){getline}}1' a.txt
one
eight
nine
ten
eleven

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 03-15-2012
Thanks kamaraj.. it works.. a simple one :)

i would like to to include the matched pattern line as well in the output . how to achieve that
# 6  
Old 03-15-2012
Code:
$ awk '/two/{print;for(i=0;i<=5;i++){getline}}1' a.txt
one
two
eight
nine
ten
eleven

# 7  
Old 03-15-2012
great.. it works

can you please provide some explanation of your oneliner.. i am trying to learn awk.

---------- Post updated at 10:19 AM ---------- Previous update was at 10:17 AM ----------

rangarasan .. sorry that was problem with my script that nullified the file.

---------- Post updated at 10:19 AM ---------- Previous update was at 10:19 AM ----------

rangarasan .. sorry that was problem with my script that nullified the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

4. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

5. Shell Programming and Scripting

Print lines that do not match the pattern

I need to print the lines that do not match a pattern. I tried using grep -v and sed -n '/pattern/!p', but both of them are not working as I am passing the pattern as variable and it can be null some times. Example ........ abcd...... .........abcd...... .........abcd......... (4 Replies)
Discussion started by: sunny1234
4 Replies

6. Shell Programming and Scripting

Print lines before and after pattern match

I am using Solaris, I want to print 3 lines before pattern match pattern 5 lines after pattern match Pattern is abcd to be searched in a.txt. Looking for the solution in sed/awk/perl. Thanks .. Input File a.txt: ================= 1 2 3 abcd 4 5 6 7 8 (7 Replies)
Discussion started by: manuswami
7 Replies

7. Shell Programming and Scripting

print lines with exact pattern match

I have in a file domain.com. 1909 IN A 1.22.33.44 domain.com. 1909 IN A 22.33.44.55 ns1.domain.com. 1699 IN A 33.44.55.66 ns2.domain.com. 1806 IN A 77.77.66.66 I need to "grep" or "awk" out the lines starting with domain.com. as follows. domain.com. 1909 IN A 1.22.33.44 domain.com.... (3 Replies)
Discussion started by: anilcliff
3 Replies

8. Shell Programming and Scripting

print chunk of lines only if there is a pattern match in between them

Hi All, Please find the sample file below: NAME ID NUMBER -------------------------------------------------------------------------------------------------- --------- abcdefgheija;lksdf ... (13 Replies)
Discussion started by: niel.verty
13 Replies

9. Shell Programming and Scripting

sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks (13 Replies)
Discussion started by: steadyonabix
13 Replies

10. Shell Programming and Scripting

Search word in a line and print earlier pattern match

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Search for: word1.word2 (Which procedure contain this word, I need procedure name in output. Expected output: procedure test1 procedure test2 procedure test3 procedure test4 ... (7 Replies)
Discussion started by: susau_79
7 Replies
Login or Register to Ask a Question

Featured Tech Videos