我之前曾问过这个问题。 Get lines between pattern1 and pattern2 into a seperate file including the line with pattern1 in shell
我正在使用awk来获得@ A-Ray所建议的我想要的东西。但是,早些时候我曾经对每个对象(大约500个)运行命令,然后使用awk来获得我想要的东西。现在,我没有这样做,而是试图将所有内容吐入文本文件,并使用awk获取单个对象的行。
所以,我的文本文件是
DEFINE OBJECT('OBJECT.TOPIC') +
* ALTDATE(2015-09-22) +
* ALTTIME(00.56.38) +
DESCR('topic object') +
GET(ENABLED) +
PUT(ENABLED) +
PROPCTL(COMPAT) +
TARGTYPE(TOPIC) +
REPLACE
DEFINE OBJECT('OBJECT1') +
* CRDATE(2015-09-22) +
* CRTIME(00.56.38) +
* CURDEPTH(0) +
CUSTOM(' ') +
DESCR('TEST') +
DISTL(NO) +
PROCESS(' ') +
SHARE +
USAGE(NORMAL) +
REPLACE
DEFINE OBJECT('OBJECT2') +
* ALTDATE(2015-09-22) +
* ALTTIME(00.56.38) +
* CURDEPTH(0) +
CUSTOM(' ') +
DESCR('TEST') +
DISTL(NO) +
PROCESS(' ') +
SHARE +
USAGE(NORMAL) +
REPLACE
DEFINE OBJECT('TCOBJECT1') +
* CRDATE(2015-09-22) +
* CRTIME(00.56.38) +
* CURDEPTH(0) +
CUSTOM(' ') +
DESCR('TEST') +
DISTL(NO) +
PROCESS(' ') +
SHARE +
USAGE(NORMAL) +
REPLACE
DEFINE OBJECT('OBJECT1TC') +
* CRDATE(2015-09-22) +
* CRTIME(00.56.38) +
* CURDEPTH(0) +
CUSTOM(' ') +
DESCR('TEST') +
DISTL(NO) +
PROCESS(' ') +
SHARE +
USAGE(NORMAL) +
REPLACE
DEFINE OBJECT('OBJECT1.TC') +
* CRDATE(2015-09-22) +
* CRTIME(00.56.38) +
* CURDEPTH(0) +
CUSTOM(' ') +
DESCR('TEST') +
DISTL(NO) +
PROCESS(' ') +
SHARE +
USAGE(NORMAL) +
REPLACE
SET RECORD +
PROFILE('OBJECT2') +
GROUP('user1') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('TCOBJECT1') +
GROUP('user3') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT1') +
GROUP('user1') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT1') +
GROUP('user2') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT1') +
GROUP('user3') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT2') +
GROUP('user3') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT1.TC') +
GROUP('user1') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT1.TC') +
GROUP('user2') +
AUTHADD(change,delete,display,alter)
我希望输出为
DEFINE OBJECT('OBJECT1') +
* CRDATE(2015-09-22) +
* CRTIME(00.56.38) +
* CURDEPTH(0) +
CUSTOM(' ') +
DESCR('TEST') +
DISTL(NO) +
PROCESS(' ') +
SHARE +
USAGE(NORMAL) +
REPLACE
SET RECORD +
PROFILE('OBJECT1') +
GROUP('user1') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT1') +
GROUP('user2') +
AUTHADD(change,delete,display,alter)
因此,如果您查看我想要的输出,我只会为OBJECT1获取块并使用OBJECT1和user3删除任何内容。
所以我正在使用的命令是
cat testawk.txt|awk -vRS="[^+]\n" 'BEGIN{printf ""}/\<OBJECT\>/&&!/\<'user3'\>/{printf $0 RT}'
但是通过使用上面的命令我得到输出为
DEFINE OBJECT('OBJECT1') +
* CRDATE(2015-09-22) +
* CRTIME(00.56.38) +
* CURDEPTH(0) +
CUSTOM(' ') +
DESCR('TEST') +
DISTL(NO) +
PROCESS(' ') +
SHARE +
USAGE(NORMAL) +
REPLACE
DEFINE OBJECT('OBJECT1.TC') +
* CRDATE(2015-09-22) +
* CRTIME(00.56.38) +
* CURDEPTH(0) +
CUSTOM(' ') +
DESCR('TEST') +
DISTL(NO) +
PROCESS(' ') +
SHARE +
USAGE(NORMAL) +
REPLACE
SET RECORD +
PROFILE('OBJECT1') +
GROUP('user1') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT1') +
GROUP('user2') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT1.TC') +
GROUP('user1') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT1.TC') +
GROUP('user2') +
AUTHADD(change,delete,display,alter)
有没有办法让OBJECT1完全匹配?
答案 0 :(得分:1)
awk
救援!
处理引号很难,最好从脚本中外化它们。使用引号消除了设置单词边界的需要(引号是边界)。
$ awk -vRS='[^+]\n' -vk="'OBJECT1'" -vu="'user3'" '$0~k && $0!~u{printf "%s",$0 RT}' file
DEFINE OBJECT('OBJECT1') +
* CRDATE(2015-09-22) +
* CRTIME(00.56.38) +
* CURDEPTH(0) +
CUSTOM(' ') +
DESCR('TEST') +
DISTL(NO) +
PROCESS(' ') +
SHARE +
USAGE(NORMAL) +
REPLACE
SET RECORD +
PROFILE('OBJECT1') +
GROUP('user1') +
AUTHADD(change,delete,display,alter)
SET RECORD +
PROFILE('OBJECT1') +
GROUP('user2') +
AUTHADD(change,delete,display,alter)