Linux 如何在POSTGRESQL中只转储INSERT语句而不使用SET/SELECTS/--注释?
仅转储表的特定行的我的bash代码:Linux 如何在POSTGRESQL中只转储INSERT语句而不使用SET/SELECTS/--注释?,linux,bash,postgresql,dump,pg-dump,Linux,Bash,Postgresql,Dump,Pg Dump,仅转储表的特定行的我的bash代码: acc_db mydb -c "create table export_table as $1" mypg_dump -d mydb -a -t export_table --data-only --column-inserts >> /tmp/export_data.sql acc_db mydb -c "drop table export_table" sed -i -e 's/export_table/'$table'/g' /tmp/ex
acc_db mydb -c "create table export_table as $1"
mypg_dump -d mydb -a -t export_table --data-only --column-inserts >> /tmp/export_data.sql
acc_db mydb -c "drop table export_table"
sed -i -e 's/export_table/'$table'/g' /tmp/export_data.sql
# where :
# $1 contains my SELECT statement
# $table is the actual name of the table I'm interest in
# acc_db is a wrapper to override the pwd insert
# mydb is ... some db
首先,我曾经在mypg_转储行上使用grep只获取INSERT语句,但是当我发现多行字段被grep删除,破坏了sql语句时,我不得不更改它。
但是现在我有了这种输出:
-- some comment
-- comment
*blank lines*
*various SET statements*
*SELECT statements*
INSERT [...]
-- some comments
*more blank lines*
我真正需要的只是INSERT语句,但我找不到任何方法只获取INSERT语句 如果要获得多行插入,则需要使用比grep更灵活的工具。使用perl一行程序的示例:
perl-wlne'if(!$current_statement){if(/^\s*INSERT/){if(/;\s*$/){push@inserts,${u}else{s/\r//g;$current_statement=$$}}}else{$current__语句。=$\s*$/{push@inserts,$current_u语句;$current_u语句=”“}}}}}{print for(@inserts)/tmp/export\sql>/sql>
正在发生的事情的细目:
#-w:使用警告
#-l:循环输入,将每行内容存储在$_
#-n:处理后不要打印行
#-e:执行一个班轮
if(!$current_语句){#如果不在多行插入中
if(/^\s*INSERT/){#如果不是INSERT语句,则跳过
如果(/;\s*$/){#单行插入,则按至列表
推动@插入件$_
}否则{#多行插入剥离CR,保存行并继续
s/\r//g;
$current_语句=$_
}
}
}否则{#继续多行现有多行插入
$current_statement.=$;#始终附加语句
如果(/;\s*$/){#如果完成,则推到@inserts,重置变量
push@inserts,$current_语句;
$current_statement=“”;
}
}
}{#“爱斯基摩之吻”:从-n完成循环,在处理文件后运行代码
打印(@inserts)#打印inserts数组的每个条目
…它是有效的,而且不是我要求的