Syntax error: "(" unexpected when using GNU sed with 'e' flag

Alex Gyoshev

Desired end result

I'm trying to convert the following (MS-SQL) string

INSERT INTO Foo (Bar) VALUES (CAST('1958-08-22 21:00:00.000' AS DateTime))

to SQLite syntax

INSERT INTO Foo (Bar) VALUES (-358491600)

Approach

I'm successfully doing this with the following sed arguments:

sed -r "s#cast\('(.*)' as datetime\)#date -d '\1' '+%s'#ige"

(calling date -d '...' '+%s' to convert the date to epoch)

Problem

Running the same command over the complete line:

echo "INSERT INTO Foo (Bar) values (cast('1958-08-22 21:00:00.000' as datetime))" | \
    sed -r "s#cast\('(.*)' as datetime\)#date -d '\1' '+%s'#ige"

...produces an error: sh: 1: Syntax error: "(" unexpected

From what I've tracked, parenthesis cause the line to fail:

echo "() cast('1958-08-22 21:00:00.000' as datetime)" | \
    sed -r "s#cast\('(.*)' as datetime\)#date -d '\1' '+%s'#ige"

Removing the e switch properly converts the command. What am I doing wrong?

Kent

this sed with ge flag does your job:

sed -r 's/(.*CAST[^\x27]*\x27)([^\x27]*)(\x27 AS DateTime.*)/
      echo "\1"$(date -d"\2" "+%s")"\3"/ge' file

with your example:

kent$  cat f
INSERT INTO Foo (Bar) VALUES (CAST('1958-08-22 21:00:00.000' AS DateTime));
INSERT INTO Foo (Bar) VALUES (CAST('1958-08-23 22:00:00.000' AS DateTime));
kent$  sed -r 's/(.*CAST[^\x27]*\x27)([^\x27]*)(\x27 AS DateTime.*)/echo "\1"$(date -d"\2" "+%s")"\3"/ge' file
INSERT INTO Foo (Bar) VALUES (CAST('-358488000' AS DateTime));
INSERT INTO Foo (Bar) VALUES (CAST('-358398000' AS DateTime));

if you don't want to have the As DateTime in output, just make proper groups, I think you can manage it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

GNU Bison: Syntax Error, unexpected <token>

From Dev

GNU Bison: Syntax Error, unexpected <token>

From Dev

sh: 1: Syntax error: redirection unexpected when using backticks perl

From Dev

dash reports 'Syntax error: "(" unexpected' when using process substitution

From Dev

Unexpected EOF when using '-c' flag on sudo

From Dev

Error when using sed

From Dev

`Syntax error: "(" unexpected` when creating an array

From Dev

-E flag for sed

From Dev

syntax error near unexpected token `&' (using "|&")

From Dev

PHP syntax error, unexpected '[' in using an array

From Dev

Sed directory not found when running R with -e flag

From Dev

Sed directory not found when running R with -e flag

From Dev

Bash: Syntax Error Near Unexpected Token `(' When Using Wgrib2

From Dev

Bash: Syntax Error Near Unexpected Token `(' When Using Wgrib2

From Dev

Syntax error when using sed to replace line-specific string in parallel: {= s/_1/_2/ =}?

From Dev

Error 'Syntax error: "(" unexpected' when declaring arrays in bash

From Dev

Error 'Syntax error: "(" unexpected' when declaring arrays in bash

From Dev

Syntax error when using views

From Dev

syntax error when using ifstream

From Dev

syntax error when using byacc

From Dev

Syntax error when using << but not when using +=

From Dev

Syntax error: redirection unexpected

From Dev

Syntax Error: unexpected Token {

From Dev

syntax error, unexpected 'endforeach'

From Dev

PHP syntax error, unexpected '['

From Dev

Wordpress Syntax Error, unexpected [

From Dev

Syntax error: "fi" unexpected

From Dev

Syntax Error: Unexpected Identifier

From Dev

Syntax error: "(" unexpected

Related Related

HotTag

Archive