Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I have a problem with a replace of a pattern in a file. Example of file: CREATE TABLE TEST.TEST ( ID_SOGGETTO BIGINT NOT NULL, ENTE VARCHAR(64)) UNIQUE PRIMARY INDEX ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2013
    Posts
    4

    Issue with replace


    Hi,
    I have a problem with a replace of a pattern in a file.

    Example of file:
    CREATE TABLE TEST.TEST
    (
    ID_SOGGETTO BIGINT NOT NULL,
    ENTE VARCHAR(64))
    UNIQUE PRIMARY INDEX ( ID_SOGGETTO ,ENTE );

    I want to replace the red pattern with another text:
    i want to replace the text after the character ")" at the end of line up to the ";" character.
    The file contain multiple CREATE TABLE statements.
    I tryed with sed command..

    Thanks
    Marco

  2. #2
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    413
    Keep playing with sed. You'll eventually get it.

    I dunno. What's the pattern? The semicolon? The brackets? The stuff that came before it? You have to find the pattern.

  3. #3
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    413
    BTW sometimes it works well to parse the original file into a tempfile that is more usable first, then run sed (or awk) on it.

    eg.
    Code:
    CREATE TABLE TEST.TEST 
    (
    ID_SOGGETTO BIGINT NOT NULL,
    ENTE VARCHAR(64))
    UNIQUE PRIMARY INDEX ( ID_SOGGETTO ,ENTE );
    to
    Code:
    CREATE TABLE TEST.TEST (ID_SOGGETTO BIGINT NOT NULL,ENTE VARCHAR(64))
    UNIQUE PRIMARY INDEX ( ID_SOGGETTO ,ENTE );
    So I have file test.j
    Code:
    CREATE TABLE TEST.TEST 
    (
    ID_SOGGETTO BIGINT NOT NULL,
    ENTE VARCHAR(64))
    UNIQUE PRIMARY INDEX ( ID_SOGGETTO ,ENTE );
    CREATE TABLE TEST.TEST 
    (
    ID_SOGGETTO BIGINT NOT NULL,
    ENTE VARCHAR(64))
    UNIQUE PRIMARY INDEX ( ID_SOGGETTO ,ENTE );
    and I use
    Code:
    cat test.j |tr -d '\n' | sed -e "s/))/))\n/g" -e "s/;/;\n/g" > test.k
    I get test.k
    Code:
    CREATE TABLE TEST.TEST (ID_SOGGETTO BIGINT NOT NULL,ENTE VARCHAR(64))
    UNIQUE PRIMARY INDEX ( ID_SOGGETTO ,ENTE );
    CREATE TABLE TEST.TEST (ID_SOGGETTO BIGINT NOT NULL,ENTE VARCHAR(64))
    UNIQUE PRIMARY INDEX ( ID_SOGGETTO ,ENTE );
    Then I go
    Code:
    cat test.k | sed "s/^.*;/STUFF/g"
    and I get
    Code:
    CREATE TABLE TEST.TEST (ID_SOGGETTO BIGINT NOT NULL,ENTE VARCHAR(64))
    STUFF
    CREATE TABLE TEST.TEST (ID_SOGGETTO BIGINT NOT NULL,ENTE VARCHAR(64))
    STUFF
    Peace and Cheer.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •