1

I'm trying to do a shell script that reads from a file from a string A to a B string. The string A I'm sure that is UNIQUE, but the B string is repeated more than one time.

I'm reading from a file that contains a lot of CREATE queries.

each query ends with (my String B)

); ------------------------

String A is composed this way:

CREATE MULTISET TABLE DBNAME.TABLENAME

so I read with sed from A to B

sed -n "/$FROMSTR/,/$TOSTR/p" $2 >> querytest.txt

I want to stop to the first occurrence of $TOSTR (String B)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Hammond95
  • 556
  • 7
  • 20

2 Answers2

3

In place of:

sed -n "/$FROMSTR/,/$TOSTR/p"

use:

sed -n "/$FROMSTR/,\${p; /$TOSTR/q}"

This prints from the first occurrence of $FROMSTR to the last line $ except that it quits when it sees the first occurrence of $TOSTR.

Aside: You should be sure that you trust the source of FROMSTR and TOSTR. If either variable contained sed-active characters, the result might not be what you want.

Example 1

As a simple example:

$ FROMSTR=2; TOSTR=4; seq 10 | sed -n "/$FROMSTR/,\${p; /$TOSTR/q}"
2
3
4

Example 2

As an exampled closer to your actual input, consider this test file:

$ cat file
1
CREATE MULTISET TABLE DBNAME.TABLENAME
2
3
); ------------------------
4

And run this command:

$ FROMSTR="CREATE MULTISET TABLE DBNAME.TABLENAME"
$ TOSTR="); ------------------------"
$ sed -n "/$FROMSTR/,\${p; /$TOSTR/q}" file
CREATE MULTISET TABLE DBNAME.TABLENAME
2
3
); ------------------------

Example 3

Consider this test file:

$ cat file
1
CREATE MULTISET TABLE DBNAME.TABLENAME
2
); ------------------------
); ------------------------
3
CREATE MULTISET TABLE DBNAME.TABLENAME
4
); ------------------------
5

We define our variables:

$ FROMSTR="CREATE MULTISET TABLE DBNAME.TABLENAME"
$ TOSTR="); ------------------------"

And, run our code:

$ sed -n "/$FROMSTR/,\${p; /$TOSTR/q}" file
CREATE MULTISET TABLE DBNAME.TABLENAME
2
); ------------------------
John1024
  • 109,961
  • 14
  • 137
  • 171
  • What are you expecting `$p` to expand to? – amphetamachine Jun 01 '16 at 16:21
  • @amphetamachine Good point. That was eliminated in the update. – John1024 Jun 01 '16 at 16:23
  • @John1024 It keeps printing all the occurrences, still something missing :/ – Hammond95 Jun 03 '16 at 08:23
  • @MartinMcFlyDeLuca I just tried it with a more complex sample file (see "Example 3" in the updated answer). I still show it printing _only_ the first occurrence. The `q` command causes `sed` to quit as soon as it reaches the first occurrence of `$TOSTR` after the first occurrence of `$FROMSTR`. Please try _Example 3_ for me and tell me if you see anything different. – John1024 Jun 03 '16 at 08:44
  • @John1024 first example could not happen, i'm sure that the file is made as create statement then );----- and so on – Hammond95 Jun 03 '16 at 08:48
  • @John1024 It prints from $FROMSTR to last occurrence of $TOSTR – Hammond95 Jun 03 '16 at 08:49
  • Example3 as well did not work It keeps printing from $FROMSTR to the last occurrence :/ – Hammond95 Jun 03 '16 at 08:58
0

I solved my problem, seems that sed has some problem in managing escape characters (\r\n)

I changed my $TOSTR to ");"

and used a loop.

sed -n "/$FROMSTR/{p; :loop n; p; /$TOSTR/q; b loop}" $2 >> $3

then i echo the characters that i need after ");"

echo -e "\r\n--------------------------------------------------------------------------------" >> $3

An useful one on stackoverflow that explain loop

Community
  • 1
  • 1
Hammond95
  • 556
  • 7
  • 20