![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to filter out some paragraphs in a file | cnlhap | Shell Programming and Scripting | 7 | 08-19-2008 04:03 PM |
| Using sed to remove paragraphs with variables | BlueberryPickle | UNIX for Dummies Questions & Answers | 1 | 07-03-2008 10:46 AM |
| how to sort paragraphs by date within a file | nabmufti | Shell Programming and Scripting | 1 | 02-13-2008 05:33 PM |
| how to extract paragraphs from file in BASH script followed by prefix ! , !! and !!! | nabmufti | Shell Programming and Scripting | 6 | 02-09-2008 08:32 PM |
| matching 3 patterns in shell script | saibsk | UNIX for Dummies Questions & Answers | 1 | 01-11-2008 03:06 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
removing certain paragraphs for matching patterns
Hi,
I have a log file which might have certain paragraphs. Switch not possible Error code 1234 Process number 678 Log not available Error code 567 Process number 874 ..... ...... ...... Now I create an exception file like this. cat text.exp Error code 1234 Process number 874 What I want is to read my original file but exclude any paragraphs that might contact the keywords written in the exception file. How can I do that? |
|
||||
|
Code:
$ cat file1 Switch not possible Error code 1234 Process number 678 Log not available Error code 567 Process number 874 Log not available Error code 333 Process number 34 Log not available Error code 33334234 Process number 012 Log not available Error code 333 hello Process number 012 Log not available Error code 567 Process number 8743434 Log not available Error code 567 Process number 874 ok Code:
$ cat text.exp Error code 1234 Process number 874 Error code 333 Code:
$ ./script.sh Log not available Error code 33334234 Process number 012 Log not available Error code 567 Process number 8743434 Code:
$ cat script.sh
#!/bin/bash
awk '
BEGIN{
RS=""; ORS="\n\n"
}
NR == FNR{
split($0, arr, "\n");
}
NR != FNR{
str = $0 "\n"
for(i=1; i <= length(arr); i++){
regx = arr[i] "[[:space:]?|\n]"
if(str ~ regx) break;
}
if(i > length(arr)) print $0
}
' text.exp file1
![]() .Aaron |
|
||||
|
I am not sure how it works but looks like this could help me. Thank you for such a short turnaround. I will check the awk script and see how it works.
Just as a doubt can't I use a while read do loop and do the same thing. Something like this... cat tem.exp | while read exception do cat file | grep -iv $exception done I know the above code is not right. But thats just a basic idea. I dont know how to remove a para in the first place. And then I need to do it in a loop so that I parse each line in the exception file. |
|
||||
|
awk:
Code:
nawk '{
if (NR==FNR){arr[$NF]++}
else{
if (FNR%3==1)
{
if($NF in arr){flag=1}
else{t=$0}
}
else if(FNR%3==2){
if($NF in arr || flag==1){flag=0;next}
else{print t;print $0}
}
else
next
}
}' text.exp file
perl: Code:
open FH,"<text.exp";
while(<FH>){
@arr=split(" ",$_);
%hash=(%hash,$arr[$#arr],$.);
}
close(FH);
open FH,"<file";
while(<FH>){
@temp=split(" ",$_);
$t=$temp[$#temp];
$line=$_ if (!exists($hash{$t}) && $.%3==1);
if(!exists($hash{$t}) && $.%3==2){
if(length($line)>0){
push(@res,$line);
push(@res,$_);
}
}
if($,%3==0){
next;
}
}
foreach (@res){
print;
}
close(FH);
Last edited by summer_cherry; 07-21-2008 at 04:57 AM.. |
![]() |
| Bookmarks |
| Tags |
| linux, linux commands, solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|