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 ?
here's something to get you started
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
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
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 02:55 AM..
Reason: added code tags
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)
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)
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)
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)
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)
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)
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)
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)
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)