How to print text between specific words(as Inputs

2019-03-05 12:06发布

I want to print complete text between specific words. Here is my requirement.

"123.log" contain below text.

EBSPRODSTART
web logic server
Oracle database administration
Linux operating system
EBSPRODEND

Output should be:-

web logic server
Oracle database administration
Linux operating system

Brief:-

Above word "EBSPROD" is database name. This value will be an input argument. e.g.

$ DBNAME=EBSPROD
$ echo $DBNAME
EBSPROD
$

Am trying like below but it is not printing any value.

$ cat 123.log |sed -n '/^$DBNAMESTART$/,/^$DBANEMEND$/p'

If am passing DBNAME value directly am getting the output as below.

$ cat 123.log |sed -n '/^EBSPRODSTART$/,/^EBSPRODEND$/p'
web logic server
Oracle database administration
Linux operating system
$

Note:- DBNAME will not be same value every time when i execute the script.

标签: shell sed
1条回答
乱世女痞
2楼-- · 2019-03-05 12:28

There are three problems with your code:

  1. '...' doesn't interpolate any variables. You need "..." for that.
  2. $DBNAMESTART looks for a variable called DBNAMESTART. You need ${DBNAME}START.
  3. DBANEMEND is not the same as DBNAMEEND.

Thus:

sed -n "/^${DBNAME}START\$/,/^${DBNAME}END\$/p"
查看更多
登录 后发表回答