Delete patterns matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete patterns matching
# 1  
Old 09-19-2017
Delete patterns matching

Delete patterns matching


OS version: RHEL 7.3
Shell : Bash

I have a file like below (pattern.txt). I need to delete all lines starting with the following words (words separated by comma below) and ) character.

LOGGING, NOCOMPRESS, TABLESPACE , PCTFREE, INITRANS, MAXTRANS, STORAGE, INITIAL, NEXT, MINEXTENTS, MAXEXTENTS, BUFFERPOOL, )
Can this be done using sed or awk ?

Code:
$ cat pattern.txt
(
  PARTITION SYS_SUBP77865
    LOGGING
    NOCOMPRESS
    TABLESPACE OSM_ORDER_DATA
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          8M
                NEXT             1M
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                BUFFER_POOL      DEFAULT
               ),
  PARTITION SYS_SUBP77866
    LOGGING
    NOCOMPRESS
    TABLESPACE OSM_ORDER_DATA
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          8M
                NEXT             1M
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                BUFFER_POOL      DEFAULT
               ),
  PARTITION SYS_SUBP77867
    LOGGING
    NOCOMPRESS
    TABLESPACE OSM_ORDER_DATA
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          8M
                NEXT             1M
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                BUFFER_POOL      DEFAULT
               ),
  PARTITION SYS_SUBP77868
    LOGGING
    NOCOMPRESS
    TABLESPACE OSM_ORDER_DATA
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          8M
                NEXT             1M
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                BUFFER_POOL      DEFAULT
               ),

Expected output:

Code:
(
  PARTITION SYS_SUBP77865
  PARTITION SYS_SUBP77866
  PARTITION SYS_SUBP77867
  PARTITION SYS_SUBP77868
  .
  .
  .

# 2  
Old 09-19-2017
Any attempts / ideas / thoughts from your side?
# 3  
Old 09-19-2017
Code:
awk 'NR==FNR {c=split($0, no_match, " *, *"); for (i=1; i<=c; i++) del_line[no_match[i]]=no_match[i] ; next}
{gsub("[)]", ") ")}
! ($1 in del_line)
' no_match.txt pattern.txt

# 4  
Old 09-25-2017
When you say "starting with" I don't see any lines that match that criteria because nearly all the lines have leading spaces. Do you really mean that you want to drop all lines that have the first non-blank/first word as those in your list?

What's wrong with something like grep -Ev "Cond1|Cond2|Cond3....." infile > outfile?

The conditions would be expressions to describe what you need to exclude. The opposite could be simpler, i.e. use grep to select only the lines you do want?



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extended grep not matching some patterns

i have a file where the hostnames and variables are in same line in below format, am able extract some part variables while otherlike subscriptions and handler is missing. can you please correct me if grep is able to perform this ? cat /tmp/test localhost subscriptions='' handler="genie"... (14 Replies)
Discussion started by: rakeshkumar
14 Replies

2. Shell Programming and Scripting

Find files not matching multiple patterns and then delete anything older than 10 days

Hi, I have multiple files in my log folder. e.g: a_m1.log b_1.log c_1.log d_1.log b_2.log c_2.log d_2.log e_m1.log a_m2.log e_m2.log I need to keep latest 10 instances of each file. I can write multiple find commands but looking if it is possible in one line. m file are monthly... (4 Replies)
Discussion started by: wahi80
4 Replies

3. Shell Programming and Scripting

Finding matching patterns in two files

Hi, I have requirement to find the matching patterns of two files in Unix. One file is the log file and the other is the error list file. If any pattern in the log file matches the list of errors in the error list file, then I would need to find the counts of the match. For example, ... (5 Replies)
Discussion started by: Bobby_2000
5 Replies

4. UNIX for Dummies Questions & Answers

Matching two patterns in the consecutive lines

Hi Experts I need to match 2 patterns consecutively and display 25 lines after that. 1st one - Error 2nd one - End string ( comes along with the pattern one) 3rd one - error Logic grep "ERROR OCCURRED :" trace.log | awk -v "ES=:" -v "SS=java.lang.NullPointerException" '{ if($NF ~... (8 Replies)
Discussion started by: senthil.ak
8 Replies

5. UNIX for Dummies Questions & Answers

Search and extract matching patterns

%%%%% (9 Replies)
Discussion started by: lucasvs
9 Replies

6. Shell Programming and Scripting

print lines between 2 matching patterns

Hi Guys, I have file like below, I want to print all lines between test1231233 to its 10 occurrence(till line 41) test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq23 test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq123131 (3 Replies)
Discussion started by: jagnikam
3 Replies

7. Shell Programming and Scripting

Matching patterns

I have a file name in $f. If $f has "-" at the beginning, or "=", or does not have extension ".ry" or ".xt" or ".dat" then cerr would not be empty. Tried the following but having some problems. set cerr = `echo $f | awk '/^-|=|!.ry|!.xt|!.dat/'` (4 Replies)
Discussion started by: kristinu
4 Replies

8. Shell Programming and Scripting

AWK: matching patterns in 2 different files

In a directory, there are two different file extensions (*.txt and *.xyz) having similar names of numerical strings (*). The (*.txt) contains 5000 multiple files and the (*.xyz) also contains 5000 multiple files. Each of the files has around 4000 rows and 8 columns, with several unique string... (5 Replies)
Discussion started by: asanjuan
5 Replies

9. Shell Programming and Scripting

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 (7 Replies)
Discussion started by: kaushys
7 Replies

10. UNIX for Dummies Questions & Answers

matching 3 patterns in shell script

IN a file I need to check for 3 patterns if all the 3 patterns are in the file. I need to send out an email. All this needs to be done in korn shell script. Please advise. (1 Reply)
Discussion started by: saibsk
1 Replies
Login or Register to Ask a Question