search string in a file and retrieve 10 lines including string line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search string in a file and retrieve 10 lines including string line
# 1  
Old 08-17-2010
search string in a file and retrieve 10 lines including string line

Hi Guys,

I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same.


can anyone of you please let me know how to go about it ?
# 2  
Old 08-17-2010
here's something to get you started
Code:
#!/usr/bin/perl

my $filename      = shift;
my $search_string = shift;
my $new_filename  = shift;
my @to_write;

open (FSEARCH, '<', $filename)  or  die "could not read file $filename : $! \n";
while (my $line = <FSEARCH>) {
    if ($line =~ m/$search_string/) {
        print "found $search_string \n";
    }
}
close (FSEARCH);

open (FWRITE, '>', $new_filename)  or  die "could not write file $new_filename : $! \n";
print FWRITE @to_write;
close (FWRITE);

__END__

q. what's not in here?
1. when the search string is found, writing the next ten lines in the array
2. write the new file only if search string is found in the given file
# 3  
Old 08-17-2010
Solution using awk

Code:
$ cat 1.txt
word1 line1 word2
word1 line2 word2
word1 line3 word2
word1 line4 word2
word1 line5 word2
word1 line6 word2
word1 line7 word2
word1 line5 word2
word1 line8 word2
word1 line9 word2

$ awk '/line5/ {for(i=0;i<4;i++) {print;if(!getline) exit;} }' 1.txt
word1 line5 word2
word1 line6 word2
word1 line7 word2
word1 line5 word2

# 4  
Old 08-18-2010
Thanks a lot guys...

Now i have the code which search for the string and put it in diff file but its executing only once evn though the name is present many at times in the file... i guess i need to give a condition to search till file ends ...

ranjithpr;

I used awk command it works fine if i want to print but can u pls tell me how to use this command so that it will print it in a file ? and also i found out that if the search string contains double cotes perl code will exit saying syntax error ... any suggestion ?

am using it like this inside perl
Code:
system "( awk '/Error "no1"/ {for(i=0;i<12;i++) {print; if(!getline) exit;} }' filename)';

or shall i use it in this way open file and use awk command to push all needful data ?

open (FSEARCH, '<', $filename) or die "could not read file $filename : $! \n";
while (my $line = <FSEARCH>) {
if ($line =~ m/$search_string/) {
@to_write = $line;


open (FWRITE, '>', $new_filename) or die "could not write file $new_filename : $! \n";
print FWRITE @to_write;
print "\n\n string has been written to $new_filename\n\n";


system "(awk '/Error "no1"/ {for(i=0;i<12;i++) {print FWRITE;if(!getline) exit;} }' $filename)";
print "\n Error Printed \n";
close (FWRITE);
}
}
close (FSEARCH);

Last edited by Yogesh Sawant; 08-25-2010 at 01:55 AM.. Reason: added code tags
# 5  
Old 08-18-2010
Hope this is what you were expecting

Code:
$ cat 1.txt
word1 line1 word2
word1 line2 word2
word1 line3 word2
word1 line4 word2
word1 "line5" word2
word1 line6 word2
word1 line7 word2
word1 "line5" word2
word1 line8 word2
word1 line9 word2
$ awk '/\"line5\"/ {for(i=0;i<4;i++) {print;if(!getline) exit;} }' 1.txt > result.txt
$ cat result.txt
word1 "line5" word2
word1 line6 word2
word1 line7 word2
word1 "line5" word2
$

# 6  
Old 08-19-2010
Thanks a lot... it worked fine !!

---------- Post updated 2010-08-19 at 11:10 AM ---------- Previous update was 2010-08-18 at 03:33 PM ----------

Hi i have one more requirement ...

is there any way i can delete 10 no of lines ..... like 2 lines above line5 and 2 lines after line5 .... any suggestion will be really helpful ..

---------- Post updated at 02:24 PM ---------- Previous update was at 11:10 AM ----------

any suggestions pls ???
# 7  
Old 08-19-2010
Code:
awk -v s=5 'NR<s-2 || NR>s+2' urfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

2. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

6. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

7. Shell Programming and Scripting

Search string and print the above line and below lines?.

if the first string matches then print the previous line and current line and also print the following lines if the other string search matches. Input ------ TranTime 2012 10 12 The Record starts here Accountnumber: 4632473431274 TxnCode 323 TranID 329473242834 ccsdkcnsdncskd... (7 Replies)
Discussion started by: laknar
7 Replies

8. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

9. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

10. Shell Programming and Scripting

Retrieve string from each line in a file based on a pattern in Unix

I have a file which contains several lines. Sample content of the file is as below. OK testmessage email<test@123> NOK receivemessage email<123@test> NOK receivemessage email(123@test123) NOK receivemessage email<abc@test> i would like to know by scripting will... (10 Replies)
Discussion started by: ramasar
10 Replies
Login or Register to Ask a Question