我想创建一个bash脚本,它只执行INSERT行的pg_dump并更改INSERT行中的表标题。
我有以下bash脚本:
#!bin/bash
#Create temp files to store the PSQL dump
DUMPFILE='poops.dump.sql' || (echo "make sql dump file failed" 1>&2; exit 1)
TMPFILE=`mktemp` || (echo "mktemp failed" 1>&2; exit 1)
#Tables to dump: api_order, poops_order_dates, poops_price
#Dump as INSERT queries statements
pg_dump --username="poops" --host="localhost" \
--table="api_order" --table="poops_order_dates" --table="poops_price" \
--no-password --column-inserts \
--data-only "poops" | \
awk '/^INSERT/ {i=1} {if(i) print}' \
> "$TMPFILE" \
|| (echo "pg_dump failed" 1>&2; exit 1)
(echo "start transaction; truncate table api_order; "; \
echo "truncate table poops_order_dates; "; \
echo "truncate table poops_price; "; \
cat "$TMPFILE"; echo 'commit;' ) \
> "$DUMPFILE" \
|| (echo "parsing dump file failed" 1>&2; exit 1)
rm "$TMPFILE"
它删除了第一个INSERT INTO行之前的所有垃圾,但是在所有INSERT INTO查询之后仍然存在以下内容:
--
-- Name: api_order_id_seq; Type: SEQUENCE SET; Schema: public; Owner: poops
--
SELECT pg_catalog.setval('api_order_id_seq', 33146, true);
--
-- Data for Name: poops_order_dates; Type: TABLE DATA; Schema: public; Owner: poops
--
--
-- Name: poops_order_dates_id_seq; Type: SEQUENCE SET; Schema: public; Owner: poops
--
SELECT pg_catalog.setval('poops_order_dates_id_seq', 1, false);
--
-- Data for Name: poops_price; Type: TABLE DATA; Schema: public; Owner: poops
--
--
-- Name: poops_price_id_seq; Type: SEQUENCE SET; Schema: public; Owner: poops
--
SELECT pg_catalog.setval('poops_price_id_seq', 1, false);
--
-- PostgreSQL database dump complete
--
如何编写以下awk行:
awk '/^INSERT/ {i=1} {if(i) print}' \
使它只输出INSERT INTO查询,并将api_order更改为api_order_test。
我确实尝试过:
awk '/^INSERT/ {gsub("INSERT INTO api_order", "INSERT INTO api_order_test", $0); print $0}' \
但是由于打印$ 0,它会切断部分查询,从新行开始。我需要它来过滤,输出和更改
"INSERT INTO api_order .... );\r"
我相信最后它是一个回车符(\ r)或pg_dump输出每个INSERT INTO查询结尾的\ n是什么?
原始SQL转储:
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET lock_timeout = 0;
--
-- Data for Name: api_order; Type: TABLE DATA; Schema: public; Owner: poops
--
INSERT INTO api_order (id, order type, …'', 0, NULL);
INSERT INTO api_order (id, order type, …'', 0, NULL);
INSERT INTO api_order (id, order type, …'', 0, NULL);
INSERT INTO api_order (id, order type, …'', 0, NULL);
INSERT INTO api_order (id, order type, …'', 0, NULL);
--
-- Name: api_order_id_seq; Type: SEQUENCE SET; Schema: public; Owner: poops
--
SELECT pg_catalog.setval('api_order_id_seq', 33294, true);
--
-- Data for Name: hoops_price; Type: TABLE DATA; Schema: public; Owner: poops
--
--
-- Name: hoops_price_id_seq; Type: SEQUENCE SET; Schema: public; Owner: poops
--
SELECT pg_catalog.setval('hoops_price_id_seq', 1, false);
--
-- PostgreSQL database dump complete
--
答案 0 :(得分:0)
如果您想要打印的所有内容都不想打印,则可以像打开它一样“关闭”打印。像/^INSERT/ {i=1} /^SELECT/ {i=0} i
这样的东西。 (请注意,尾随i
等同于您的{ if (i) print }
)如果输入更复杂或有其他注意事项,请将输入的代表性样本发布到awk和预期输出。
- 雅