Print lines that do not match the pattern

[LEFT]I need to print the lines that do not match a pattern. I tried using grep -v and sed -n '/pattern/!p', but both of them are not working as I am passing the pattern as variable and it can be null some times.

Example

........ abcd[1234]......
.........abcd[2341]......
.........abcd[1234]......
.........abcd[4567]......
.........abcd[9876]......

would need the output to be not matching pattern 1234, so it would be

.........abcd[2341]......
.........abcd[4567]......
.........abcd[9876]......

[/LEFT]

The value in the brackets is passed as a variable and sometimes it can be null
so the grep or sed is not returning any value. when it is supposed to.

Please help.

Check if pattern is defined and then grep for it:

pattern="1234"

[[ ! -z "$pattern" ]] && grep -v "\[$pattern\]" file

Hi

Thank you for the quick reply, this option would not work as the structure of the line is different, as below.

For other purposes, in the whole script, I am extracting the value in the brackets and assigning it to a variable. The value in the brackets is alpha numeric with "-" in between ex: abcd[xyz12-45de-5678se].

I am extracting the value based on a condition. so sometimes it can be null. In that case, the grep will look like grep -v ' '

Thanks

Your requirement is not so clear. The command should have worked for the sample data that you posted!

I think it will help if you can post few lines from your original input file and desired output in code tags

try also:

awk '! ($0 ~ "[[]" 1234 "[]]")' infile