4 votes

Fortnightly Programming Q&A Thread

General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.

Don't forget to format your code using the triple backticks or tildes:

Here is my schema:

```sql
CREATE TABLE article_to_warehouse (
  article_id   INTEGER
, warehouse_id INTEGER
)
;
```

How do I add a `UNIQUE` constraint?

3 comments

  1. [3]
    tempestoftruth
    (edited )
    Link
    I'm learning bash using this tutorial and am trying to practice using regular expressions with sed. I have a document with several lines in a numbered list, i.e. each line in question begins with...

    I'm learning bash using this tutorial and am trying to practice using regular expressions with sed. I have a document with several lines in a numbered list, i.e. each line in question begins with a number of 1-3 digits in length, digits between 0-9, followed by a period, a space, and then text.

    2. John Smith
    54. David Smith
    347. Joe Smith
    633. Henry Smith
    

    I want to use sed to delete this first portion of each of these lines, so that the data looks like this:

    John Smith
    David Smith
    Joe Smith
    Henry Smith
    

    First, I ran the following line of code to get an idea of the lines I'd be selecting, beginning with lines that start with three digits:

    egrep '[0-9]{3}\.' input.txt

    I wrote the following line of code to actually edit lines starting with three digits:

    sed 's/[0-9]{3}\. //g' input.txt > output.txt

    Yet when I open output.txt in vi the contents of the file haven't changed from input.txt. I assumed the problem was that I was writing my regular expression incorrectly, and so sed wasn't selecting the strings I specified, but using a regular expression online service suggested my regular expression was written correctly, and the problem must be elsewhere.

    It's unclear to me where exactly my error is. Any help would be appreciated!

    1 vote
    1. [2]
      Deimos
      (edited )
      Link Parent
      One of the annoying things about regex is that quite a few variants exist, and almost all of them have slightly different idiosyncrasies. In sed's case, one of them is that you have to escape...

      One of the annoying things about regex is that quite a few variants exist, and almost all of them have slightly different idiosyncrasies. In sed's case, one of them is that you have to escape curly brackets with a backslash. So it would need to be:

      sed 's/[0-9]\{3\}\. //g' input.txt > output.txt
      

      That's not quite right still though - the \{3\} makes it match exactly 3 numbers, but you want to match 1-3, so it should be \{1,3\}:

      sed 's/[0-9]\{1,3\}\. //g' input.txt > output.txt
      

      (You can also call sed with -E to use "extended regular expressions" so that the backslashes aren't needed and {1,3} would have worked, but in the end it's just different idiosyncrasies)

      1. tempestoftruth
        Link Parent
        Oh, that's quite confusing for a beginner, especially when the tutorial doesn't tell you egrep is using extended regex. Thank you for the clarification though, and for the tip on using {1,3}...

        Oh, that's quite confusing for a beginner, especially when the tutorial doesn't tell you egrep is using extended regex. Thank you for the clarification though, and for the tip on using {1,3} instead of {3}!

        1 vote