Shreya
Shreya

Reputation: 649

search through regex pattern in a file

I have a file with data in below format

abc {u_bit_top/connect_down/u_FDIO[6]/u_latch}
ghi {u_bit_top/seq_connect/p_REDEIO[9]/ff_latch
def {u_bit_top/connect_up/shift_reg[7]

I want to search for pattern *bit_top*FDIO* and *bit_top*REDEIO*in the file in each line and delete the complete line if pattern is found. I want output as

def {u_bit_top/connect_up/shift_reg[7]

I did using sed like sed "/bit_top/d;/FDIO/d;/REDEIO/d;" but this deletes the line having bit_top and FDIO and REDEIO separately. How I can search for above pattern and delete the line containing it. Shell or TCL anything will be useful.

Upvotes: 1

Views: 99

Answers (6)

flash
flash

Reputation: 161

You can read the file line by line and perform not operation on your regex pattern.

set fp [open "input.txt" r]
while { [gets $fp data] >= 0 } {

    if {![regexp {bit_top.*(FDIO|REDEIO)} $data match]}
    {puts $match}

}
close $fp

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246942

Since you tagged

set fh [open "filename"]
set contents [split [read -nonewline $fh] \n]
close $fh
set filtered [lsearch -inline -not -regexp $contents {bit_top.*(FDIO|REDEIO)}]

results in

def {u_bit_top/connect_up/shift_reg[7]

lsearch documentation.


But really all you need for this is

grep -Ev 'bit_top.*(FDIO|REDEIO)' filename

Upvotes: 4

markp-fuso
markp-fuso

Reputation: 34698

With a small change you can implement the compound pattern (eg, *bit_top*FDIO*) in sed.

A couple variations on OP's current sed:

# daisy-chain the 2 requirements:

$ sed "/bit_top.*FDIO/d;/bit_top.*REDEIO/d" file
def {u_bit_top/connect_up/shift_reg[7]

# enable "-E"xtended regex support:

$ sed -E "/bit_top.*(FDIO|REDEIO)/d" file
def {u_bit_top/connect_up/shift_reg[7]

Upvotes: 0

sseLtaH
sseLtaH

Reputation: 11237

Using sed

$ sed -E '/bit_top.*(REDE|FD)IO/d' input_file
def {u_bit_top/connect_up/shift_reg[7]

Upvotes: 2

Daweo
Daweo

Reputation: 36560

You might use GNU AWK for this task following way, let file.txt content be

abc {u_bit_top/connect_down/u_FDIO[6]/u_latch}
ghi {u_bit_top/seq_connect/p_REDEIO[9]/ff_latch
def {u_bit_top/connect_up/shift_reg[7]

then

awk '/bit_top/&&(/FDIO/||/REDEIO/){next}{print}' file.txt

gives output

def {u_bit_top/connect_up/shift_reg[7]

Explanation: if lines contain bit_top AND (FDIO OR REDEIO) then go to next line i.e. skip it. If that did not happen line is just printed.

(tested in GNU Awk 5.0.1)

Upvotes: 1

boppy
boppy

Reputation: 1908

You've been close! ;)

sed '/bit_top.*FDIO/d' input

Just input a regex to sed that matches what you want...

Upvotes: 3

Related Questions