LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-19-2014, 10:10 AM   #1
slik
LQ Newbie
 
Registered: Jun 2014
Posts: 4

Rep: Reputation: Disabled
Excluding square bracket pattern from file


Hello,

From my file I want to exclude lines that contain ] not preceded by [

file1:
aa]bb -->should be excluded
aa]bb] -->should be excluded
aa][bb -->should be excluded
aa[bb] -->should not be excluded
aa -->should not be excluded

Grep -v [^[]*] does not do it for me.


I'm missing the obvious here.
Many thanks for helping me.
 
Old 06-19-2014, 12:13 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
How about something like:
Code:
sed -n '/\]/{/\[.*\]/p};/\]/!p' file
 
1 members found this post helpful.
Old 06-20-2014, 03:29 AM   #3
slik
LQ Newbie
 
Registered: Jun 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Grail, you saved my day!
works perfectly, nice one.

much appreciated.
 
Old 06-20-2014, 04:45 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,847

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
grail you continue to educate me.
Let's hope there is only ever one set of square brackets.
 
Old 06-20-2014, 10:29 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Quote:
Let's hope there is only ever one set of square brackets.
True, but it was not in the original brief
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Square bracket hoax w1k0 Programming 14 05-19-2013 11:11 AM
Scripting- sed command - replacing strings excluding the lines matching a pattern(s) msriram Linux - General 1 11-05-2012 10:29 AM
"Missing right curly or square bracket at -e" error in perl nivedhitha Programming 7 08-31-2011 04:17 AM
[SOLVED] Filename beginning with left bracket: file cannot be erased. stf92 Linux - Newbie 2 11-28-2010 03:00 AM


All times are GMT -5. The time now is 05:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration