如何使用带有模式的bash删除文件中的行块,并将剩余的行保存在另一个文件中

时间:2017-06-15 06:34:03

标签: linux bash shell awk

我目前正在处理一个脚本,该脚本会删除 file1.txt 中具有特定模式的行块,并将剩余的行保存在 file2.txt 上。以下是 file1.txt

的示例
dn: fssecPermissionId=FSNASVIEW,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: FSNASVIEW
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_1

dn: fssecPermissionId=FSNASMANAGE,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: FSNASMANAGE
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_2

dn: fssecPermissionId=permission1,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: permission1
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_3

dn: fssecPermissionId=CMPlanManagementmonitor,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: CMPlanManagementmonitor
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_4

dn: fssecPermissionId=permission2,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: permission2
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_5

我想删除fssecPermissionId=FSNASVIEWfssecPermissionId=FSNASMANAGEfssecPermissionId=CMPlanManagementmonitor

的块
  

请注意,fssecPermissionId有超过70个值   我想删除,我只是举了几个简单的例子。我是   还想到那70个值会以某种方式存储在数组中   但我不知道怎么做。

以下是将保存到 file2.txt

的预期输出
dn: fssecPermissionId=permission1,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: permission1
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_3

dn: fssecPermissionId=permission2,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: permission2
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_5

非常感谢您提前寻求帮助。

2 个答案:

答案 0 :(得分:1)

您可以awk使用空RS

awk -v RS= -v ORS='\n\n' '!/fssecPermissionId=(FSNASVIEW|FSNASMANAGE|CMPlanManagementmonitor)/' file

dn: fssecPermissionId=permission1,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: permission1
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_3

dn: fssecPermissionId=permission2,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: permission2
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_5

要保存更改,请检查:awk save modifications in place

编辑:如果您正在处理大量排除模式,请创建一个名为excl.txt的文件,其中包含您要排除的所有字符串,如下所示:

cat excl.txt
FSNASVIEW
FSNASMANAGE
CMPlanManagementmonitor

然后使用这个awk命令:

awk -F, -v ORS='\n\n' 'NR==FNR {
   ex["dn: fssecPermissionId=" $1]
   next
}
!($1 in ex)' excl.txt RS= file

dn: fssecPermissionId=permission1,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: permission1
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_3

dn: fssecPermissionId=permission2,fsFragmentId=Permissions,fsFragmentId=Securi
 ty,fsClusterId=ClusterRoot
fssecPermissionId: permission2
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_5

答案 1 :(得分:0)

文件:

dn: fssecPermissionId=FSNASVIEW,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
fssecPermissionId: FSNASVIEW
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_1

dn: fssecPermissionId=FSNASMANAGE,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
fssecPermissionId: FSNASMANAGE
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_2

dn: fssecPermissionId=permission1,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
fssecPermissionId: permission1
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_3

dn: fssecPermissionId=CMPlanManagementmonitor,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
fssecPermissionId: CMPlanManagementmonitor
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_4

dn: fssecPermissionId=permission2,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
fssecPermissionId: permission2
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_5

sed

sed -r /"fssecPermissionId=(FSNASVIEW|FSNASMANAGE|CMPlanManagementmonitor)"/,+4d file

输出:

dn: fssecPermissionId=permission1,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
fssecPermissionId: permission1
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_3

dn: fssecPermissionId=permission2,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
fssecPermissionId: permission2
objectClass: FSSecPermission
fssecAssignedGroupRoleRef: group_5