12437

Sed: Why does [^\]] not seem to work?

<h3>Question</h3>

I was trying to match/output all the text before the first ] appeared:

sed -i -r 's/^([^\]]*).*/\1/' "$FILE.tmp"

Which surprisingly does not work.

However, searching for the other bracket does work:

sed -i -r 's/^([^\[]*).*/\1/' "$FILE.tmp"

Is this a sed bug or am I doing it wrong?

I know I can work around this using .*? which is good enough but this issue had me stumped for long enough that I'd like to know if there is something I missed.

(According --version, I am using GNU sed 4.2.2.)


<h3>Answer1:</h3>

You don't need to quote the closing bracket ]. In fact your regular expression is parsed as [^\] followed by a stray ].

You want to use the following regular expression (note the lack of quoting with \):

sed -i -r 's/^([^]].*).*/\1/' "$FILE.tmp"

The second regular expression works by chance because [ has no special meaning in a [] expression but you'll find that it also match lines that start by a \.

In fact, when using ] in a [] group, it must be the first character, or the second if the first one is a ^.


<h3>Answer2:</h3>

It should be:

sed -r 's/^([^]]+).*/\1/'

Note that you don't need to quote the ] if it appears in a character group.

来源:https://stackoverflow.com/questions/35275040/sed-why-does-not-seem-to-work

Recommend

  • Internal server error in Google Composer web UI [Error code 28]
  • Whats the difference between Unhealthy vs Degraded .NET health check status
  • Flutter firebase validation of form field inputs
  • What does (int_1 += *pointer++ = int_2++) < int_3 mean?
  • Is multiple inheritance possible in VB .Net?
  • Current way of importing in a mixed Objective-C / Swift project
  • iPhone iOS: SQLite3 Not able to get last ID value in Table
  • Change message body before sending in outlook 365
  • Optimising HDF5 dataset for Read/Write speed
  • Why do I get IPC delays on 20% busy machine
  • Tap Gesture on List View Items
  • How to test Rate this app functionality before submitting to app store
  • Changing UITableViewCell's width constraint's constant value for each instance
  • Grid on top of Google maps produces gaps in squares
  • Copy and reformat cells from one worksheet to another
  • r - insert row for missing monthly data and interpolate
  • Get all the values from excel file by using linqtoexcel
  • Errors with Codename One “Send iOS Build” and “Send Android Build”
  • Auto Increment / Identity Custom Field Type
  • How to add ng-app after page load?
  • Validating a Firebase Key [duplicate]
  • Jekyll - How do I create pages in the root directory?
  • create circular Auto Horizontal Scroll View?
  • android : speech recognition what are the technologies available
  • Auto send email based on the time and email address in database
  • How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to s
  • Multiple canvases (pages) in Fabric.js
  • Python, SimPy: Using yield inside functions
  • content must have a ListView whose id attribute is 'android.R.id.list'
  • Create an Office365 mailbox from within C# Web API method
  • Julia 1.0 UndefVarError - Scope of Variable
  • Java Collections.shuffle() weird behaviour [closed]
  • How to use AJAX to upload large CSV file? [closed]
  • Find angle of point on circle
  • How to merge objects within array based on attribute
  • VS2010 RDLC C#. How can I set a LocalReport object to a ReportViewer?
  • I am consuming a WCF service that requires headers from a .NET 2 website. How can I programmatically
  • how to get the location(lat/lng) on google maps v3 from the location(x,y)
  • How to use FirstOrDefault inside Include
  • Time Complexity of Fibonacci Algorithm [duplicate]