|
Prev: Changning the partition type in a Bash-Script
Next: Tilde home directory variable name expansion problem
From: newhorizon on 19 Jun 2008 06:45 can anyone help me to figure out what this "ed" script is trying to do ? and how to convert it to to use "sed" ? cat - <<EOF|ed -s indfile.tmp 1,$ g/REM /s/REM //g 1,$ g/INITIAL *$/j 1,$ g/INITIAL *[0-9]* /s/INITIAL *[0-9]* /INITIAL 100K /g 1,$ g/NEXT *$/j 1,$ g/NEXT *[0-9]* /s/NEXT *[0-9]* /NEXT 100K /g 1,$ g/PCTINCREASE *$/j 1,$ g/PCTINCREASE *[0-9]* /s/PCTINCREASE *[0-9]* /PCTINCREASE 0 /g w tables.sql q EOF
From: Dave B on 19 Jun 2008 08:55 newhorizon wrote: > can anyone help me to figure out what this "ed" script is trying to > do ? > > and how to convert it to to use "sed" ? > > > > cat - <<EOF|ed -s indfile.tmp > 1,$ g/REM /s/REM //g > 1,$ g/INITIAL *$/j > 1,$ g/INITIAL *[0-9]* /s/INITIAL *[0-9]* /INITIAL 100K /g > 1,$ g/NEXT *$/j > 1,$ g/NEXT *[0-9]* /s/NEXT *[0-9]* /NEXT 100K /g > 1,$ g/PCTINCREASE *$/j > 1,$ g/PCTINCREASE *[0-9]* /s/PCTINCREASE *[0-9]* /PCTINCREASE 0 /g > w tables.sql > q > EOF It's sending some commands to ed. If memory serves, these commands are meant to operate on the file "indfile.tmp", and write the changed output to the file "tables.sql". First, it changes all the strings "REM " to nothing. Then, it performs a similar operation on three different groups of lines: - lines that end with INITIAL (and any number of spaces after that) are joined with the subsequent line, which presumably has a numeric value at the beginning; then, on each newly joined line, "INITIAL "+the number are replaced with "INITIAL 100K ". - lines that end with NEXT (and any number of spaces after that) are joined with the subsequent line, which presumably contains a numeric value at the beginning; then, on each newly joined line, "NEXT "+the number are replaced with "NEXT 100K ". - lines that end with PCTINCREASE (and any number of spaces after that) are joined with the subsequent line, which presumably contains a numeric value at the beginning; then, on each newly joined line, "PCTINCREASE "+the number are replaced with "PCTINCREASE 0 ". Finally, the output is written to tables.sql. You don't provide a sample input and output resulting from the ed script, so it's difficult to test. However, the following sed code /should/ be almost equivalent (assuming I correctly interpreted the ed script, which might not be the case): $ cat ed.sed /INITIAL *$/ { N;s/\n//; s/INITIAL *[0-9]* /INITIAL 100K /g; be } /NEXT *$/ { N;s/\n//; s/NEXT *[0-9]* /NEXT 100K /g; be } /PCTINCREASE *$/ { N;s/\n//; s/PCTINCREASE *[0-9]* /PCTINCREASE 0 /g; be } :e s/REM //g; $ sed -f ed.sed indfile.tmp > tables.sql The difference is that ed apparently does a pass over the whole input for each command, while sed does only a single pass. The sed solution assumes that each substitution is applied to a different set of lines, ie there are no lines that can match more than one pattern (excluding "REM "), neither before nor after having been joined with the next line. That may or may not make a difference, depending on the actual input, which you can post if you want, so that the solution can be tested against real data. -- echo 0|sed 's909=oO#3u)o19;s0#0ooo)].O0;s()(0bu}=(;s#}#.1m"?0^2{#; s)")9v2@3%"9$);so%op]t(p$e#!o;sz(z^+.z;su+ur!z"au;sxzxd?_{h)cx;:b; s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/; tb'|awk '{while((i+=2)<=length($1)-18)a=a substr($1,i,1);print a}'
From: newhorizon on 19 Jun 2008 10:38 the following is my test data. Thank you very much for the explanatory answer. It helped a lot :) TABLE "REPCAT$_SNAPGROUP" CREATE TABLE "REPCAT$_SNAPGROUP" ("GOWNER" VARCHAR2(30), "GNAME" VARCHAR2(30), "DBLINK" VARCHAR2(128), "GROUP_COMMENT" VARCHAR2(80), "REP_TYPE" NUMBER, "FLAVOR_ID" NUMBER) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING NOCOMPRESS ALTER TABLE "REPCAT$_SNAPGROUP" MODIFY DEFAULT B^@ALTER TABLE "REPCAT$_SNAPGROUP" MODIFY ("GOWNER" DEFAULT 'PUBLIC') CREATE UNIQUE INDEX "I_REPCAT$_SNAPGROUP1" ON "REPCAT $_SNAPGROUP" ("GNAME" , "DBLINK" , "GOWNER" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING COMMENT ON TABLE "REPCAT$_SNAPGROUP" IS ,^@'Snapshot repgroup registration information' COMMENT ON COLUMN "REPCAT$_SNAPGROUP"."GOWNER" IS ^@'Owner of the snapshot repgroup' COMMENT ON COLUMN "REPCAT$_SNAPGROUP"."GNAME" IS ^_^@'Name of the snapshot repgroup' COMMENT ON COLUMN "REPCAT$_SNAPGROUP"."DBLINK" IS (^@'Database site of the snapshot repgroup' COMMENT ON COLUMN "REPCAT$_SNAPGROUP"."GROUP_COMMENT" IS &^@'Description of the snapshot repgroup' COMMENT ON COLUMN "REPCAT$_SNAPGROUP"."REP_TYPE" IS "^@'Identifier of flavor at snapshot' TABLE "REPCAT$_TEMPLATE_OBJECTS" CREATE TABLE "REPCAT$_TEMPLATE_OBJECTS" ("TEMPLATE_OBJECT_ID" NUMBER NOT NULL ENABLE, "REFRESH_TEMPLATE_ID" NUMBER NOT NULL ENABLE, "OBJECT_NAME" VARCHAR2(30) NOT NULL ENABLE, "OBJECT_TYPE" NUMBER NOT NULL ENABLE, "OBJECT_VERSION#" NUMBER, "DDL_TEXT" CLOB, "MASTER_ROLLBACK_SEG" VARCHAR2(30), "DERIVED_FROM_SNAME" VARCHAR2(30), "DERIVED_FROM_ONAME" VARCHAR2(30), "FLAVOR_ID" NUMBER, "SCHEMA_NAME" VARCHAR2(30), "DDL_NUM" NUMBER NOT NULL ENABLE, "TEMPLATE_REFGROUP_ID" NUMBER NOT NULL ENABLE, "FLAGS" RAW(255), "SPARE1" VARCHAR2(4000)) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING NOCOMPRESS LOB ("DDL_TEXT") STORE AS (TABLESPACE "SYSTEM" ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10 NOCACHE STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1)) ALTER TABLE "REPCAT$_TEMPLATE_OBJECTS" MODIFY DEFAULT D^@ALTER TABLE "REPCAT$_TEMPLATE_OBJECTS" MODIFY ("DDL_NUM" DEFAULT 1 ) ALTER TABLE "REPCAT$_TEMPLATE_OBJECTS" MODIFY DEFAULT Q^@ALTER TABLE "REPCAT$_TEMPLATE_OBJECTS" MODIFY ("TEMPLATE_REFGROUP_ID" DEFAULT 0 ) CREATE UNIQUE INDEX "REPCAT$_TEMPLATE_OBJECTS_PK" ON "REPCAT $_TEMPLATE_OBJECTS" ("TEMPLATE_OBJECT_ID" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING CREATE UNIQUE INDEX "REPCAT$_TEMPLATE_OBJECTS_U1" ON "REPCAT $_TEMPLATE_OBJECTS" ("OBJECT_NAME" , "OBJECT_TYPE" , "REFRESH_TEMPLATE_ID" , "SCHEMA_NAME" , "DDL_NUM" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING CREATE INDEX "REPCAT$_TEMPLATE_OBJECTS_N1" ON "REPCAT $_TEMPLATE_OBJECTS" ("REFRESH_TEMPLATE_ID" , "OBJECT_TYPE" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING CREATE INDEX "REPCAT$_TEMPLATE_OBJECTS_N2" ON "REPCAT $_TEMPLATE_OBJECTS" ("REFRESH_TEMPLATE_ID" , "OBJECT_NAME" , "SCHEMA_NAME" , "OBJECT_TYPE" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING CALTER TABLE ýÿ*^@ALTER TABLE "REPCAT$_TEMPLATE_OBJECTS" ADD*^@ CONSTRAINT "REPCAT $_TEMPLATE_OBJECTS_VER"^H^@ CHECK (0^@object_version# >= 0 AND object_version# < 65536^A^@)^R^@ ENABLE NOVALIDATE^@^@ ALTER TABLE "REPCAT$_TEMPLATE_OBJECTS" ADD CONSTRAINT "REPCAT $_TEMPLATE_OBJECTS_PK" PRIMARY KEY ("TEMPLATE_OBJECT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING ENABLE ALTER TABLE "REPCAT$_TEMPLATE_OBJECTS" ADD CONSTRAINT "REPCAT $_TEMPLATE_OBJECTS_U1" UNIQUE ("OBJECT_NAME", "OBJECT_TYPE", "REFRESH_TEMPLATE_ID", "SCHEMA_NAME", "DDL_NUM") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING ENABLE COMMENT ON COLUMN "REPCAT$_TEMPLATE_OBJECTS"."TEMPLATE_OBJECT_ID" IS =^@'Internal primary key of the REPCAT$_TEMPLATE_OBJECTS table.' COMMENT ON COLUMN "REPCAT$_TEMPLATE_OBJECTS"."REFRESH_TEMPLATE_ID" IS >^@'Internal primary key of the REPCAT$_REFRESH_TEMPLATES table.' COMMENT ON COLUMN "REPCAT$_TEMPLATE_OBJECTS"."OBJECT_NAME" IS ^^^@'Name of the database object.' COMMENT ON COLUMN "REPCAT$_TEMPLATE_OBJECTS"."OBJECT_TYPE" IS ^Z^@'Type of database object.' COMMENT ON COLUMN "REPCAT$_TEMPLATE_OBJECTS"."OBJECT_VERSION#" IS &^@'Version# of database object of TYPE.'
From: Dave B on 19 Jun 2008 10:48 newhorizon wrote: > the following is my test data. Thank you very much for the explanatory > answer. It helped a lot :) > > TABLE "REPCAT$_SNAPGROUP" >[snip] There are some strange things about this: 1) it seems that NO line would be processed, since no line ends with INITIAL, NEXT or PCTINCREASE. The latter two words do not occur at all, while the lines that have INITIAL do not have it at the end. Also, no line has "REM ". 2) many lines have most certainly been wrapped, so neither script can be run on the data. 2) can be solved by pasting the real data on some pastebin site. But the strange thing is 1). Are you sure that what you posted is the data the ed script was meant to run on? -- echo 0|sed 's909=oO#3u)o19;s0#0ooo)].O0;s()(0bu}=(;s#}#.1m"?0^2{#; s)")9v2@3%"9$);so%op]t(p$e#!o;sz(z^+.z;su+ur!z"au;sxzxd?_{h)cx;:b; s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/; tb'|awk '{while((i+=2)<=length($1)-18)a=a substr($1,i,1);print a}'
From: Michael Tosch on 19 Jun 2008 11:33
newhorizon wrote: > can anyone help me to figure out what this "ed" script is trying to > do ? > > and how to convert it to to use "sed" ? > > > > cat - <<EOF|ed -s indfile.tmp > 1,$ g/REM /s/REM //g > 1,$ g/INITIAL *$/j > 1,$ g/INITIAL *[0-9]* /s/INITIAL *[0-9]* /INITIAL 100K /g > 1,$ g/NEXT *$/j > 1,$ g/NEXT *[0-9]* /s/NEXT *[0-9]* /NEXT 100K /g > 1,$ g/PCTINCREASE *$/j > 1,$ g/PCTINCREASE *[0-9]* /s/PCTINCREASE *[0-9]* /PCTINCREASE 0 /g > w tables.sql > q > EOF > Why cat? ed -s indfile.tmp << EOF .... EOF I am not so familiar with ed. But g/PATTERN/ s/PATTERN/... is IMHO the same as just s/PATTERN/... So I would translate it into: sed ' s/REM //g /INITIAL *$/N s/INITIAL *[0-9]* /INITIAL 100K /g /NEXT *$/N s/NEXT *[0-9]* /NEXT 100K /g /PCTINCREASE *$/N s/PCTINCREASE *[0-9]* /PCTINCREASE 0 /g ' indfile.tmp > tables.sql -- echo imhcea\.lophc.tcs.hmo | sed 's2\(....\)\(.\{5\}\)2\2\122;s1\(.\)\(.\)1\2\11g;1s;\.;::;2' |